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.