Skip to content
Rishan Solutions
Rishan Solutions
  • PowerApps
  • SharePoint online
    • Uncategorized
    • Uncategorized
  • PowerAutomate
Rishan Solutions
Latest Posts
  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025 June 24, 2025
  • Recursive Queries in T-SQL May 7, 2025
  • Generating Test Data with CROSS JOIN May 7, 2025
  • Working with Hierarchical Data May 7, 2025
  • Using TRY_CAST vs CAST May 7, 2025
  • Dynamic SQL Execution with sp_executesql May 7, 2025

Monitoring External Sharing in SharePoint Online using PnP PowerShell

Posted on March 18, 2025March 18, 2025 by Rishan Solutions

Loading

1. Introduction

External sharing in SharePoint Online allows users to collaborate with people outside the organization. However, improper management of external sharing can lead to security risks and data leaks.

Using PnP PowerShell, we can:
Retrieve external sharing settings for SharePoint Online sites
Identify externally shared files and folders
Export an external sharing report to CSV
Automate external sharing monitoring


2. Prerequisites

Before you begin, ensure you have:

  • PnP PowerShell installed
    If not, install it using: Install-Module -Name PnP.PowerShell -Scope CurrentUser -Force
  • SharePoint Admin or Global Admin permissions
  • The SharePoint Admin Center URL of your organization

3. Connecting to SharePoint Online

Before retrieving external sharing details, connect to SharePoint Online.

Step 1: Connect to SharePoint Online

$AdminURL = "https://yourtenant-admin.sharepoint.com"
Connect-PnPOnline -Url $AdminURL -Interactive
  • Replace "yourtenant" with your SharePoint tenant name.
  • This prompts you to log in with Microsoft 365 credentials.

4. Retrieving External Sharing Settings

To check whether external sharing is enabled:

$SharingSettings = Get-PnPTenantSite | Select-Object Url, SharingCapability
$SharingSettings | Format-Table -AutoSize
  • This command lists all SharePoint sites and their external sharing status.

Understanding SharingCapability Values:

ValueDescription
DisabledExternal sharing is not allowed
ExistingExternalUserSharingOnlyOnly existing guests can access
ExternalUserSharingOnlyAnyone with a Microsoft or work/school account can access
ExternalUserAndGuestSharingAnonymous links and external users are allowed

5. Identifying Shared Files and Folders

To list all files and folders shared externally:

$SiteURL = "https://yourtenant.sharepoint.com/sites/YourSite"
Connect-PnPOnline -Url $SiteURL -Interactive

$SharedItems = Get-PnPListItem -List "Documents" -Fields "FileRef", "FileLeafRef", "SharedWithUsers"
$SharedItems | Where-Object { $_.SharedWithUsers -ne $null } | Select-Object FileRef, SharedWithUsers
  • Replace "YourSite" with your actual site name.
  • This retrieves files in the Documents library that are shared externally.

6. Exporting External Sharing Report to CSV

To generate a report of all externally shared files and users:

Step 1: Connect to SharePoint Online

$SiteURL = "https://yourtenant.sharepoint.com/sites/YourSite"
Connect-PnPOnline -Url $SiteURL -Interactive

Step 2: Retrieve Shared Files and Export to CSV

$SharedItems = Get-PnPListItem -List "Documents" -Fields "FileRef", "FileLeafRef", "SharedWithUsers"
$Results = @()

foreach ($Item in $SharedItems) {
if ($Item.SharedWithUsers -ne $null) {
$Results += [PSCustomObject]@{
FileName = $Item.FileLeafRef
FilePath = $Item.FileRef
SharedWith = ($Item.SharedWithUsers -join "; ")
}
}
}

$Results | Export-Csv -Path "C:\Reports\ExternalSharingReport.csv" -NoTypeInformation
Write-Host "External Sharing Report Exported Successfully!"
  • This script extracts:
    File name
    File location
    Users it is shared with
  • The report is saved as ExternalSharingReport.csv in C:\Reports\.

7. Automating the External Sharing Monitoring Process

To schedule automated monitoring, create a PowerShell script (Monitor-ExternalSharing.ps1) and run it periodically.

Step 1: Save the Script

$SiteURL = "https://yourtenant.sharepoint.com/sites/YourSite"
Connect-PnPOnline -Url $SiteURL -Interactive

$SharedItems = Get-PnPListItem -List "Documents" -Fields "FileRef", "FileLeafRef", "SharedWithUsers"
$Results = @()

foreach ($Item in $SharedItems) {
if ($Item.SharedWithUsers -ne $null) {
$Results += [PSCustomObject]@{
FileName = $Item.FileLeafRef
FilePath = $Item.FileRef
SharedWith = ($Item.SharedWithUsers -join "; ")
}
}
}

$Results | Export-Csv -Path "C:\Reports\ExternalSharingReport.csv" -NoTypeInformation
Write-Host "External Sharing Report Exported Successfully!"

Step 2: Schedule the Task

  1. Open Task Scheduler.
  2. Click Create Basic Task.
  3. Choose a Trigger (e.g., daily or weekly).
  4. Select Action > Start a Program.
  5. Set Program/Script to powershell.exe.
  6. In Add Arguments, enter: -File "C:\Path\To\Monitor-ExternalSharing.ps1"
  7. Click Finish and enable the task.

Now, SharePoint external sharing will be monitored automatically.

Posted Under PNP PowerShellaccess control external sharing Microsoft 365 PNP PowerShell PowerShell Audit Security Compliance SharePoint Governance SharePoint monitoring SharePoint online SharePoint Security

Post navigation

Microsoft.Extensions.Options.OptionsValidationException – Configuration options validation failed
Microsoft.Extensions.DependencyInjection.Abstractions.ServiceProvider – Unable to resolve service

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025
  • Recursive Queries in T-SQL
  • Generating Test Data with CROSS JOIN
  • Working with Hierarchical Data
  • Using TRY_CAST vs CAST

Recent Comments

  1. Michael Francis on Search , Filter and Lookup in power apps
  2. A WordPress Commenter on Hello world!

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • March 2024
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023
  • April 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • January 2022

Categories

  • Active Directory
  • AI
  • AngularJS
  • Blockchain
  • Button
  • Buttons
  • Choice Column
  • Cloud
  • Cloud Computing
  • Data Science
  • Distribution List
  • DotNet
  • Dynamics365
  • Excel Desktop
  • Extended Reality (XR) – AR, VR, MR
  • Gallery
  • Icons
  • IoT
  • Java
  • Java Script
  • jQuery
  • Microsoft Teams
  • ML
  • MS Excel
  • MS Office 365
  • MS Word
  • Office 365
  • Outlook
  • PDF File
  • PNP PowerShell
  • Power BI
  • Power Pages
  • Power Platform
  • Power Virtual Agent
  • PowerApps
  • PowerAutomate
  • PowerPoint Desktop
  • PVA
  • Python
  • Quantum Computing
  • Radio button
  • ReactJS
  • Security Groups
  • SharePoint Document library
  • SharePoint online
  • SharePoint onpremise
  • SQL
  • SQL Server
  • Template
  • Uncategorized
  • Variable
  • Visio
  • Visual Studio code
  • Windows
© Rishan Solutions 2026 | Designed by PixaHive.com.
  • Rishan Solutions
Go to mobile version