Tuesday, June 23, 2015

SharePoint 2010 PowerShell: Batch assign permissions to each list item

PowerShell script to add permissions to all items in a list or library, where each item already has its unique set of permissions. Useful when you have hundreds of items in your library and can't be bothered to assign one by one.

$webUrl = "http://sharepoint"
$listName = ""
$folderName = ""
$sc = New-Object Microsoft.SharePoint.SPSite($webURL)
$web = $sc.OpenWeb()
$list = $web.Lists[$listName]
#Assign read permissions to the site group - faster
$permission = $web.RoleDefinitions["Read"];
$principal = $web.SiteGroups[""]; (if single user, use $web.Users[""]
$assignment = new-object Microsoft.SharePoint.SPRoleAssignment($principal)
$assignment.RoleDefinitionBindings.Add($permission)
foreach ($item in $list.Items)
{
write-host $item.Title
$itemAssignment = $item.RoleAssignments
$itemAssignment.Add($assignment)
write-host "Permission granted"
}

$web.Dispose()


No comments:

Related Posts Plugin for WordPress, Blogger...