Is there any PowerShell or other way to delete unique permissions on all sub-folders/sub-sub-folders/files, in a specific parent folder, and have everything under the parent folder inherit permissions? SharePoint Online.

frob 4,261 Reputation points
2022-08-10T14:22:54.953+00:00

Hi there
In a SharePoint Online Document Library:
Is there any PowerShell or other way to delete unique permissions on all sub-folders/sub-sub-folders/files, in a specific parent folder, and have everything under the parent folder inherit permissions from it?

Thanks.

Microsoft 365 and Office SharePoint For business Windows
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Emily 76 Reputation points
    2022-08-11T07:41:28.45+00:00

    @frob

    1.You could reset unique permissions for all files in a folder by using below PnP PowerShell.

       #Parameters  
        $SiteURL = "https://crescent.sharepoint.com/sites/Marketing"  
        $FolderServerRelativeURL = "/sites/Marketing/Branding/2018"  
    
        #Connect to the site  
        Connect-PnPOnline -Url $SiteURL -Interactive  
    
        #Get the Folder from given URL  
        $Folder = Get-PnPFolder -Url $FolderServerRelativeURL -Includes ListItemAllFields.ParentList  
        $ParentList = $Folder.ListItemAllFields.ParentList.Title  
    
        #Get All Files from the Folder  
        $Files = Get-PnPListItem -List $ParentList -FolderServerRelativeUrl $Folder.ServerRelativeUrl | Where {$_.FileSystemObjectType -eq "File"}  
    
        #Traverse through each file in the folder  
        ForEach ($File in $Files)  
        {  
            #Check If File has Unique Permissions  
            $HasUniquePermissions = Get-PnPProperty -ClientObject $File -Property HasUniqueRoleAssignments  
            If($HasUniquePermissions)  
            {  
                #Reset Broken Inheritance  
                $File.ResetRoleInheritance()  
                $File.update()  
                Invoke-PnPQuery  
                Write-Host "Reset Unique Permissions on File $($File.FieldValues["FileRef"])" -ForegroundColor Green  
            }  
        }  
    

    2.You could run below PowerShell to remove unique permissions on one folder.

     #Load SharePoint CSOM Assemblies  
     Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
     Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
    
     #Variables  
     $SiteURL = "https://tenant.sharepoint.com/sites/emilytest"  
     $FolderServerRelativeUrl= "/Sites/emilytest/Shared Documents/2022"  
    
     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 web from URL  
         $Web = $Ctx.web  
         $Ctx.Load($Web)  
         $Ctx.executeQuery()  
    
         #Get the Folder object by Server Relative URL  
         $Folder = $Web.GetFolderByServerRelativeUrl($FolderServerRelativeUrl)  
         $Ctx.Load($Folder)  
         $Ctx.ExecuteQuery()  
    
         #Reset Folder Permissions  
         $Folder.ListItemAllFields.ResetRoleInheritance()  
         $Ctx.ExecuteQuery()      
         Write-host -f Green "Folder's Unique Permissions are Removed!"  
     }  
     Catch {  
         write-host -f Red "Error Resetting Folder Permissions!" $_.Exception.Message  
     }  
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.