Environment: SharePoint Server 2016 On Premise
Requirement: To remove specific user permission from all folder, documents from a document library.
Code:
$myweb = Get-SPWeb "https://sharepoint.domain.com/SM"
$mylist = $myweb.Lists | Where-Object { $_.Title -eq 'Documents' }
#foreach ($item in $mylist.Items ){
$account = $myweb.EnsureUser("c:0+.w|s-1-5-21-790525478-117609710-839522115-1145")
$Folder = $mylist.RootFolder
foreach ($SubFolder in $mylist.Folders)
{
if ($SubFolder.HasUniqueRoleAssignments)
{
if($SubFolder.Name -ne "Forms")
{
write-host "Removing account" $user "from FOLDER " $SubFolder.Name
$SubFolder.RoleAssignments.RemoveById($account.ID)
$query = New-Object -Type 'Microsoft.SharePoint.SPQuery'
$query.Folder = $SubFolder.Folder
$folderItems = $mylist.GetItems($query)
foreach($item in $folderItems)
{
if ($item.HasUniqueRoleAssignments)
{
write-host "Removing account" $user "from FILE" $item.Url
$item.RoleAssignments.RemoveById($account.ID)
}
}
}
}
}
"#######################################################"
#}
Requirement: To remove specific user permission from all folder, documents from a document library.
Code:
$myweb = Get-SPWeb "https://sharepoint.domain.com/SM"
$mylist = $myweb.Lists | Where-Object { $_.Title -eq 'Documents' }
#foreach ($item in $mylist.Items ){
$account = $myweb.EnsureUser("c:0+.w|s-1-5-21-790525478-117609710-839522115-1145")
$Folder = $mylist.RootFolder
foreach ($SubFolder in $mylist.Folders)
{
if ($SubFolder.HasUniqueRoleAssignments)
{
if($SubFolder.Name -ne "Forms")
{
write-host "Removing account" $user "from FOLDER " $SubFolder.Name
$SubFolder.RoleAssignments.RemoveById($account.ID)
$query = New-Object -Type 'Microsoft.SharePoint.SPQuery'
$query.Folder = $SubFolder.Folder
$folderItems = $mylist.GetItems($query)
foreach($item in $folderItems)
{
if ($item.HasUniqueRoleAssignments)
{
write-host "Removing account" $user "from FILE" $item.Url
$item.RoleAssignments.RemoveById($account.ID)
}
}
}
}
}
"#######################################################"
#}