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

#Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing/"
$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)
    $Ctx.Load($Folder)
    $Ctx.ExecuteQuery()
      
    #Check if Folder has unique permissions
    $Folder.ListItemAllFields.Retrieve("HasUniqueRoleAssignments")
    $Ctx.ExecuteQuery()    
    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:
#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

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.