0

I have a document library which has 3 folders in it and inside each folder there is a document (docx, xls, ppt).

My objective is to iterate every file inside my document library and check if It has unique permissions and If it does it should remove them.

For the folder part I already have this code working properly which fetches the root folder and prints if it has unique permissions.

$siteUrl = "https://**.sharepoint.com/sites/**"
Connect-PnPOnline –Url $siteURL –Credentials **
$context = Get-PnPContext 
$ListName="List name"

$list = Get-PnPList $ListName
$folders = $list.RootFolder.Folders
$context.Load($folders);
$context.ExecuteQuery();

foreach($folder in $folders)
{
  if($folder.ItemCount -gt 0)
  {    
    $f = Get-PnPFolder -Url $folder.ServerRelativeUrl -Includes ListItemAllFields.RoleAssignments, ListItemAllFields.HasUniqueRoleAssignments

    $context.Load($f);
    $context.ExecuteQuery();
    Write-Host $f.ServerRelativeUrl -> $f.ListItemAllFields.HasUniqueRoleAssignments
  }
}   

For the document / item part I'm struggling a bit because I cant seem to put this to work. It always gives me that the item is not part of a list.

ERROR

So my questions is:

How do I check if a item / document has unique permissions and how to remove them?

1 Answer 1

0

Delete Unique Permissions from 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 -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

enter image description here

2
  • This answer is amazing and works perfectly for folders! Thank you! I'll be trying to do the items part
    – G.F
    May 6, 2021 at 9:50
  • Hi, G.F. Thanks for your reply.Is there anything else I can help with regarding this issue? You can comment us at any time and we will continue to follow up. May 10, 2021 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.