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 Folder has Unique Permissions:
#Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing/"
$FolderServerRelativeURL = "/sites/marketing/Shared Documents/2018"

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

#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:
#Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing/"
$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
SharePoint Online: Check If a Folder has Unique Permissions SharePoint Online: Check If a Folder has Unique Permissions Reviewed by Salaudeen Rajack on June 26, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.