
Managing permissions in SharePoint Online can be a challenging task, especially when dealing with items that have unique permissions. To simplify this process, we can use PowerShell to generate a report of all items with unique permissions in the document libraries of a SharePoint Online site. In this blog post, we’ll walk through a PowerShell script that accomplishes this task.
Why Monitor Unique Permissions?
Unique permissions can complicate permission management in SharePoint. While they allow for granular control, they can also lead to inconsistencies and make it harder to maintain a secure and organized environment. Regularly auditing unique permissions helps ensure that your SharePoint site remains manageable and secure.
PowerShell Script to Generate Unique Permissions Report
Below is the PowerShell script that generates a report of items with unique permissions in all document libraries of a SharePoint Online site.
Import-Module SharePointPnPPowerShellOnline
# Variables
$SiteUrl = "https://mstalk.sharepoint.com/sites/YourSite"
$OutputFile = "D:\Scriptsoutput\UniquePermissionsReport.csv"
# Connect to SharePoint Online
Connect-PnPOnline -Url $SiteUrl
# Get all document libraries in the site
$Libraries = Get-PnPList | Where-Object { $_.BaseTemplate -eq 101 } # BaseTemplate 101 is for document libraries
$Report = @()
# Loop through each document library
foreach ($Library in $Libraries) {
Write-Host "Processing library: $($Library.Title)" -ForegroundColor Cyan
# Get all items in the library
$Items = Get-PnPListItem -List $Library.Title -PageSize 1000 -Fields FileRef, FileLeafRef, HasUniqueRoleAssignments
foreach ($Item in $Items) {
if ($Item["HasUniqueRoleAssignments"] -eq $true) {
$Entry = [PSCustomObject]@{
LibraryName = $Library.Title
ItemName = $Item["FileLeafRef"]
ItemUrl = $Item["FileRef"]
HasUniquePermissions = $Item["HasUniqueRoleAssignments"]
}
$Report += $Entry
}
}
}
# Display the report in a grid view
$Report | Out-GridView -Title "Unique Permissions Report"
# Export the report to a CSV file
#$Report | Export-Csv -Path $OutputFile -NoTypeInformation -Encoding UTF8
Write-Host "Unique permissions report generated successfully"
This PowerShell script is a powerful tool for SharePoint administrators to audit and manage unique permissions in document libraries. By regularly running this script, you can maintain better control over your SharePoint Online environment and ensure that permissions are applied consistently.
Feel free to customize the script to suit your specific needs, and don’t forget to share your feedback or improvements in the comments below!






No Comments