SharePoint Reset Unique Permissions (Inherit Permissions) on a List/Library Using PowerShell
## CLEAR AND ADD SNAP IN ##
cls
Remove-PSSnapin Microsoft.SharePoint.
Add-PSSnapin Microsoft.SharePoint.
}
## ASSIGN VARIABLE ##
$webUrl = $args[0]
$listName = $args[1]
$listInherits = $args[2]
# Varibale to hold document count
$count = 0
## OPEN OBJECTS & RESTORE INHERITANCE ##
try {
# Open the web and list objects
$web = Get-SPWeb $webUrl
$list = $web.Lists[$listName]
# If the list should inherit, reset the role inheritance
if ($listInherits -eq $true) {
$list.ResetRoleInheritance()
Write-Host "Updated permissions on list." -foregroundcolor Green
}
# Get all items with unique permissions
$itemsWithUniquePermissions = $list.GetItemsWithUniquePermissions()
Write-Host $itemsWithUniquePermissions.Count "number of items with unique permissions found."
# Only update items if some exist
if ($itemsWithUniquePermissions.Count -gt 0) {
foreach ($itemInfo in $itemsWithUniquePermissions) {
$item = $list.GetItemById($itemInfo.Id)
$item.ResetRoleInheritance()
$count++
}
# Display number of items updated
Write-Host "Updated permissions on $count items." -foregroundcolor Green
}
else {
Write-Host "No items with unique permissions exist, nothing to update."
}
# Dispose of web object
$web.Dispose()
}
catch [Exception] {
Write-Host "Exception encountered. Please ensure all arguments are valid." -foregroundcolor Red
Write-Host $_.Exception.Message -foregroundcolor Red
}
Comments
Post a Comment