SharePoint Online: Check If a Folder has Unique Permissions

Requirement: Check If a Folder has Unique Permissions in SharePoint Online using PowerShell

Powershell to Check If a Folder has Unique Permissions in SharePoint Online

Check if Folder has Broken Permission Inheritance using PowerShell CSOM

Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking

$SiteURL = ""
$FolderServerRelativeURL = "/sites/marketing/Branding/2018"

Try {
    #Get Credentials to connect
    $Cred= Get-Credential
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    #Get the Folder
    $Folder = $Ctx.Web.GetFolderByServerRelativeUrl($FolderServerRelativeURL)
    #Check if Folder has unique permissions
    Write-host -ForegroundColor Green "Folder has unique Permissions:" $Folder.ListItemAllFields.HasUniqueRoleAssignments
Catch {
    write-host -f Red "Error:" $_.Exception.Message

PowerShell to Check If Folder has Unique Permissions:

$SiteURL = ""
$FolderServerRelativeURL = "/sites/marketing/Shared Documents/2018"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get Folder has unique permissions
$Folder = Get-PnPFolder -Url $FolderServerRelativeURL -Includes ListItemAllFields.HasUniqueRoleAssignments
Write-Host $Folder.ListItemAllFields.HasUniqueRoleAssignments 

Get Permissions of All Folders in SharePoint Online Library:

$SiteURL = ""
$ListSiteRelativeURL = "/Shared Documents"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get Folder permissions recursively
Function Get-FolderPermissions($FolderURL)
    #Get all sub-folders of the Folder
    $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType Folder | Where {$_.Name -ne "Forms" -and $_.Name -ne "Document"} 
    #Loop through the sub-folders
    ForEach($SubFolder in $SubFolders)
        #Calculate site relative URL of the Folder
        $SubFolderURL = $FolderUrl+"/"+$SubFolder.Name

        #Get Folder HasUniqueRoleAssignments property
        $Folder = Get-PnPFolder -Url $SubFolder.ServerRelativeUrl -Includes ListItemAllFields.HasUniqueRoleAssignments
        Write-Host "Folder '$($SubFolder.ServerRelativeUrl)' has unique permissions:" $Folder.ListItemAllFields.HasUniqueRoleAssignments

        #Call the function recursively
        Get-FolderPermissions $SubFolderURL
#Call the function
Get-FolderPermissions $ListSiteRelativeURL

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply