1

I want to remove all rights to a specific folder in SharePoint document library, all but a group.

So, I need to first find the folder and break inheritance on it. How to do it?

I use the following function, that searches subfolders if they have "private" name, it should break the inheritance and then set access to a specific group.

function RestrictFolderAccess($folderUrl) {
    Write-Output "Starting to process folders in: $folderUrl"
    $subFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $folderUrl -ItemType Folder -Recursive

    foreach ($folder in $subFolders) {
        Write-Output "Checking folder: $($folder.ServerRelativeUrl)"

        if ($folder.Name -eq "private" -or $folder.Name -eq "6- private") {
            Write-Output "Found target folder: $($folder.ServerRelativeUrl)"
            $folderContext = $folder.Context
            $folderContext.Load($folder.ListItemAllFields)
            $folderContext.ExecuteQuery()

            if ($null -ne $folder.ListItemAllFields.Id) {
                Write-Output "Breaking inheritance for: $($folder.ServerRelativeUrl)"
                Set-PnPListItemPermission -List "MyDocs" -Identity $folder.ListItemAllFields.Id -BreakRoleInheritance -ClearSubscopes $true

however, when I launch this script it says:

Set-PnPListItemPermission : A parameter cannot be found that matches parameter name 'BreakRoleInheritance'.

1 Answer 1

2

You have to use the Set-PnPFolderPermission PnP PowerShell command instead of Set-PnPListItemPermission.

Example:

Set-PnPFolderPermission -List 'Shared Documents' -Identity 'Shared Documents/Folder' -User '[email protected]' -AddRole 'Contribute' -ClearExisting

Above command adds the 'Contribute' permission to the user '[email protected]' for the folder named 'Folder' located in the root of the library 'Shared Documents' and removes all other permissions.

-ClearExisting parameter clears all existing permissions of SharePoint document library folder.

Documentation: Set-PnPFolderPermission

4
  • would the inherited permissions be removed?
    – serge
    Nov 20, 2023 at 10:22
  • Yes, using -ClearExisting parameter should remove it. Nov 20, 2023 at 10:24
  • Hi @serge, did you try above solution? Is it working for you? Nov 21, 2023 at 12:51
  • 1
    worked the variant Set-PnPFolderPermission -List $libraryName -Identity $folder.ServerRelativeUrl -Group "MyGroup" -AddRole "Collaboration" -ClearExisting thanks!
    – serge
    Nov 21, 2023 at 13:00

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.