Find All Shared Links in SharePoint Online Document Library

Requirement: We wanted to find all links shared through the “Share” Feature in SharePoint Online.

When a file or folder is shared in SharePoint Online, It generates a Link, and users can use that link to view or edit based on permissions set during Share in SharePoint Online. You may want to audit all shared links at times.

sharepoint online sharing link report

Here is the PowerShell script to generate a report for all shared links in a SharePoint Online document library:

#Parameters
$SiteUrl = "https://crescentintranet.sharepoint.com/sites/Marketing"
$ReportOutput = "C:\Temp\SharedLinks.csv"
$ListName = "Branding"
   
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
$Ctx = Get-PnPContext
$Results = @()
$global:counter = 0
 
#Get all list items in batches
$ListItems = Get-PnPListItem -List $ListName -PageSize 2000
$ItemCount = $ListItems.Count
  
#Iterate through each list item
ForEach($Item in $ListItems)
{
    Write-Progress -PercentComplete ($global:Counter / ($ItemCount) * 100) -Activity "Getting Shared Links from '$($Item.FieldValues["FileRef"])'" -Status "Processing Items $global:Counter to $($ItemCount)";

    #Check if the Item has unique permissions
    $HasUniquePermissions = Get-PnPProperty -ClientObject $Item -Property "HasUniqueRoleAssignments"
    If($HasUniquePermissions)
    {        
        #Get Shared Links
        $SharingInfo = [Microsoft.SharePoint.Client.ObjectSharingInformation]::GetObjectSharingInformation($Ctx, $Item, $false, $false, $false, $true, $true, $true, $true)
        $ctx.Load($SharingInfo)
        $ctx.ExecuteQuery()
        
        ForEach($ShareLink in $SharingInfo.SharingLinks) 
        {
            If($ShareLink.Url)
            {            
                If($ShareLink.IsEditLink)
                {
                    $AccessType="Edit"
                }
                ElseIf($shareLink.IsReviewLink)
                {
                    $AccessType="Review"
                }
                Else
                {
                    $AccessType="ViewOnly"
                }
                
                #Collect the data
                $Results += New-Object PSObject -property $([ordered]@{ 
                Name  = $Item.FieldValues["FileLeafRef"]            
                RelativeURL = $Item.FieldValues["FileRef"]
                FileType = $Item.FieldValues["File_x0020_Type"]
                ShareLink  = $ShareLink.Url
                ShareLinkAccess  =  $AccessType
                ShareLinkType  = $ShareLink.LinkKind
                AllowsAnonymousAccess  = $ShareLink.AllowsAnonymousAccess
                IsActive  = $ShareLink.IsActive
                Expiration = $ShareLink.Expiration
                })
            }
        }
    }
    $global:counter++
}
$Results | Export-CSV $ReportOutput -NoTypeInformation
Write-host -f Green "Sharing Links Report Generated Successfully!"

This PowerShell generates a CSV report with details:

  • Name of the File or Folder
  • URL of the File or Folder
  • File Type
  • Share Link
  • Access type – Contribute or Read
  • Is Anonymous Access
  • Expiration Date, etc.

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply