Sharepoint - Add group to Library permissions using powershell

This script should do the trick after few adjustments as its adding new permissions to group which you don't want so you can skip them.

function Add-SPPermissionToListGroup
{
    param ($Url, $ListName, $GroupName, $PermissionLevel)
    $web = Get-SPWeb -Identity $Url
    $list = $web.Lists.TryGetList($ListName)
    if ($list -ne $null)
    {
        // Ensure that the permissions are not being inherited.
        if ($list.HasUniqueRoleAssignments -eq $False)
        {
            $list.BreakRoleInheritance($True)
        }

        // Modify the permissions.
        if ($web.SiteGroups[$GroupName] -ne $null)
        {
            $group = $web.SiteGroups[$GroupName]
            $roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
            $roleDefinition = $web.RoleDefinitions[$PermissionLevel];
            $roleAssignment.RoleDefinitionBindings.Add($roleDefinition);
            $list.RoleAssignments.Add($roleAssignment)
            $list.Update();
            Write-Host "Successfully added $PermissionLevel permission to $GroupName group in $ListName list. " -foregroundcolor Green
        }
        else
        {
            Write-Host "Group $GroupName does not exist." -foregroundcolor Red
        }

    }
    $web.Dispose()
}

Source


Just an addendum to this as I came across this post while researching an issue we were having, if you call $List.Update() right after the RoleAssignments.Add(), you may hit a race condition and it'll report a save conflict on the $List item.

it will however update the RoleAssignment as the .Add commits the change without the need to call .Update() on the list.

Paul.