Share via

PowerShell Script to list all the files and folders that are being shared in a document library

Xia, Henry 0 Reputation points
2025-02-10T20:27:03.1333333+00:00

Hello,

I am trying to find out which folders or files on a Document Library are being shared, either internally or externally, and who was it shared to.

Is there a powershell script that does this?

Best.

Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

2 answers

Sort by: Most helpful
  1. Pratik Darak 16 Reputation points
    2025-09-02T07:15:32.3333333+00:00

    Hi @AllenXu-MSFT ,

    Could you please provide details of Get-PnPFileSharingInfo function as this command is not available in powershell module.

    Was this answer helpful?

    0 comments No comments

  2. AllenXu-MSFT 24,991 Reputation points Moderator
    2025-02-11T02:25:19.5733333+00:00

    Hi @Xia, Henry,

    Try this PnP PowerShell script.

    # Import the PnP PowerShell module
    Import-Module PnP.PowerShell
    
    # Variables
    $siteUrl = "https://yourdomain.sharepoint.com/sites/yoursite" # Replace with your SharePoint site URL
    $libraryName = "Documents" # Replace with the name of your document library
    $adminUsername = "******@yourdomain.com" # Replace with your admin username
    $adminPassword = "YourPassword" # Replace with your admin password
    
    # Connect to SharePoint Online
    Connect-PnPOnline -Url $siteUrl -Credentials (Get-Credential -UserName $adminUsername -Password (ConvertTo-SecureString $adminPassword -AsPlainText -Force))
    
    # Function to list shared files and folders in a library
    function Get-SharedItemsInLibrary {
        param (
            [string]$libraryName
        )
    
        # Get all items in the library
        $items = Get-PnPListItem -List $libraryName -PageSize 1000
    
        foreach ($item in $items) {
            $itemType = $item.FileSystemObjectType # File or Folder
            $itemUrl = $item.FieldValues.FileRef # Server-relative URL of the item
    
            # Check if the item is shared
            $sharingInfo = Get-PnPFileSharingInfo -Url $itemUrl
    
            if ($sharingInfo.SharingLinks -or $sharingInfo.SharedWithUsers) {
                Write-Host "Shared $($itemType): $($itemUrl)"
    
                # Check for sharing links
                if ($sharingInfo.SharingLinks) {
                    Write-Host " Shared via links:"
                    foreach ($link in $sharingInfo.SharingLinks) {
                        Write-Host " - Link: $($link.Url)"
                    }
                }
    
                # Check for users the item is shared with
                if ($sharingInfo.SharedWithUsers) {
                    Write-Host " Shared with users:"
                    foreach ($user in $sharingInfo.SharedWithUsers) {
                        Write-Host " - User: $($user.UserPrincipalName)"
                    }
                }
            }
        }
    }
    
    # Call the function to list shared items in the library
    Get-SharedItemsInLibrary -libraryName $libraryName
    
    # Disconnect from SharePoint Online
    Disconnect-PnPOnline
    
    
    

    Remember to replace parameters with your own and ensure you have admin permission to the SharePoint site.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.