I know it's possible to modify a library authorizations using the SharePoint UI following the steps bellow:
Stop authorizations inheritance.
Modify users authorizations.
Select/Unselect the desired checkboxes.
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"];
$list.BreakRoleInheritance($true,$true);
$group = $web.Groups["Members of the site"];
$roleToAdd = "Reader";
$role = $web.RoleDefinitions | where {$_.Type -eq $roleToAdd};
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($group);
$assignment.RoleDefinitionBindings.Add($role);
$list.RoleAssignments.Add($assignment); // How to remove instead ?
$list.Update();
$web.Dispose();
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);
$list.RoleAssignments[1].ImportRoleDefinitionBindings($assignment.RoleDefinitionBindings);
$list.Update();