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.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,201 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,809 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AllenXu-MSFT 23,591 Reputation points Microsoft Vendor
    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.


Your answer

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