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:

$SiteUrl = ""
$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"
        #Get Shared Links
        $SharingInfo = [Microsoft.SharePoint.Client.ObjectSharingInformation]::GetObjectSharingInformation($Ctx, $Item, $false, $false, $false, $true, $true, $true, $true)
        ForEach($ShareLink in $SharingInfo.SharingLinks) 
                #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
$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 - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

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

  • I too want to know if we can get Shared By and Shared To. I’ve looked in PnP and just can’t seem to find it. Any help for this type of report would be amazing!

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

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

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

  • 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.

    • Hi Tom, were you able to find how to output the person who are members of the shared links?

      • I would like this functionality too. I tried adding
        SharedBy = $ShareLink.SharedBy
        to see if that worked and came back empty.

  • 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.


Leave a Reply

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