SharePoint Online: What PowerShell can detect cases where an item has the same permission as its parent, delete unique permissions, and have it inherit permissions from its parent?

frob 4,216 Reputation points
2022-08-31T19:06:39.96+00:00

Hi

In my SharePoint Online document libraries, several items (files and folders) have unique permissions while they should inherit from their parent folder as their parent folders have exactly the same permissions.
What PowerShell can detect each such case where an item has the same permission as its parent, delete unique permissions, and have it inherit permissions from its parent?

Thanks.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,606 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,361 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Emily Du-MSFT 41,771 Reputation points Microsoft Vendor
    2022-09-01T09:06:51.853+00:00

    @frob

    Based on your description, I understand that you want to delete all unique permissions for files and folder in a document library.

    1.Remove unique permissions for all folders in a document library.

    #Set Variables  
    $SiteURL = "https://crescent.sharepoint.com/sites/marketing/2018"  
    $FolderURL = "/Shared Documents" #Document Library Site Relative URL  
    
    #Connect to PnP Online  
    Connect-PnPOnline -Url $SiteURL -Interactive  #-Credentials (Get-Credential)  
    
    #Function to reset permissions of all Sub-Folders  
    Function Reset-SubFolderPermissions($FolderURL)  
    {  
        #Get all sub-folders of the Folder - Exclude system folders  
        $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType Folder | Where {$_.Name -ne "Forms" -and $_.Name -ne "Document"}  
    
        #Loop through each sub-folder  
        ForEach($SubFolder in $SubFolders)  
        {  
            $SubFolderURL = $FolderUrl+"/"+$SubFolder.Name  
            Write-host -ForegroundColor Green "Processing Folder '$($SubFolder.Name)' at $SubFolderURL"  
    
            #Get the Folder Object - with HasUniqueAssignments and ParentList properties  
            $Folder = Get-PnPFolder -Url $SubFolderURL -Includes ListItemAllFields.HasUniqueRoleAssignments, ListItemAllFields.ParentList, ListItemAllFields.ID  
    
            #Get the List Item of the Folder  
            $FolderItem = $Folder.ListItemAllFields  
    
            #Check if the Folder has unique permissions  
            If($FolderItem.HasUniqueRoleAssignments)  
            {  
                #Reset permission inheritance  
                Set-PnPListItemPermission -List $FolderItem.ParentList -Identity $FolderItem.ID -InheritPermissions  
                Write-host "`tUnique Permissions are removed from the Folder!"  
            }  
    
            #Call the function recursively  
            Reset-SubFolderPermissions $SubFolderURL  
        }  
    }  
    
    #Call the function  
    Reset-SubFolderPermissions $FolderURL  
    

    2.Remove unique permissions for all files in a document library.

    #Set Variables  
    $SiteURL = "https://crescent.sharepoint.com/sites/Marketing"  
    $ListName = "Documents"  
    
    #Connect to PnP Online  
    Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)  
    
    #Get all list items in batches  
    $ListItems = Get-PnPListItem -List $ListName -PageSize 500  
    
    #Iterate through each list item  
    ForEach($ListItem in $ListItems)  
    {  
        #Check if the Item has unique permissions  
        $HasUniquePermissions = Get-PnPProperty -ClientObject $ListItem -Property "HasUniqueRoleAssignments"  
        If($HasUniquePermissions)  
        {         
            $Msg = "Deleting Unique Permissions on {0} '{1}' at {2} " -f $ListItem.FileSystemObjectType,$ListItem.FieldValues["FileLeafRef"],$ListItem.FieldValues["FileRef"]  
            Write-host $Msg  
            #Delete unique permissions on the list item  
            Set-PnPListItemPermission -List $ListName -Identity $ListItem.ID -InheritPermissions  
        }  
    }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.