I know it's possible to modify a library authorizations using the SharePoint UI following the steps bellow:

  1. Stop authorizations inheritance.

  2. Modify users authorizations.

  3. Select/Unselect the desired checkboxes.

  4. OK.

I want to add Reader authorization and remove Contributor.

How to achieve that using PowerShell?

So far, I succeeded to add new authorizations with PowerShell. Here is the code.

$web = Get-SPWeb "http://www.whatever";         
$list = $web.Lists["MyList"];
$group = $web.Groups["Members of the site"];                
$roleToAdd = "Reader";
$role = $web.RoleDefinitions | where {$_.Type -eq $roleToAdd};                      
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($group);         
$list.RoleAssignments.Add($assignment); // How to remove instead ?

Note: I do know that at least one authorization should be selected.

A solution could be to use this method. Unfortunately it's not working.

$roleToLet = "Reader"
$role = $web.RoleDefinitions | where {$_.Type -eq roleToLet };                      
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($group);         

The list assignments are obtained using the wrong method. Moreover, the assignments were not updated after modifications. Finally I used $web.SiteGroups instead of $web.Groups. It should be as following:

$group = $web.SiteGroups["Members of the site"];
$rolesToRemove = "Contributor";
$role = $web.RoleDefinitions | where {$_.Type -eq $rolesToRemove};                      
$assignment = list.RoleAssignments.GetAssignmentByPrincipal($group);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.