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://crescent.sharepoint.com/sites/Marketing"
$ReportOutput = "C:\Temp\SharedLinks.csv"
$ListName = "Branding"
   
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
$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 - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

6 thoughts on “Find All Shared Links in SharePoint Online Document Library

  • Will this work with OneDrive Business? Consumer?
    I often see SharePoint and OneDrive (Business) mentioned in the same sentence.

    Reply
    • Yes! Just change the site URL to OneDrive site URL and Listname to “Documents” library .

      Reply
  • This is a requirement of my company as well. Was there any follow up?

    Reply
  • Also is there a way to output the person sharing the file and who the file was shared with? This would allow us to have a good audit report that users could validate the file sharing is still valid.

    Reply
  • Is it possible to filter the results so only files that were shared with people that do not have permissions to see the file already. We have people that sharing files via email or chat with team members to make it easier to find the file. We are looking for only sharing events that were done with inside users that normally would not have access to the files. Thanks for putting this script together.

    Reply

Leave a Reply

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