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'.