1

I cannot find the Powershell commands to share a site/site collection with a user or group of users. Same as Site Settings > Site Permission. Can you help me? Thanks

1 Answer 1

1

For this PowerShell script I have created a function called AddGroupToSite, allowing you to assign a SharePoint group (must already be created in the site collection) to a site along with a permission level by specifying one line of script. This first section sets up the SPWeb object and the function:

$web = Get-SPWeb "http://portal"

function AddGroupToSite ($web, $groupName, $permLevel)
{
    $account = $web.SiteGroups[$groupName]
    $assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
    $role = $web.RoleDefinitions[$permLevel]
    $assignment.RoleDefinitionBindings.Add($role);
    $web.RoleAssignments.Add($assignment)
}

Once we have our function set up, we can call it and pass the relevant parameters as follows:

AddGroupToSite -web $web -groupName "Site Admins" -permLevel "Full Control"
AddGroupToSite -web $web -groupName "Site Readers" -permLevel "Read"

Adding SharePoint groups with permission levels to sites using PowerShell

Add a SharePoint or AD group/user to all sites in a site collection using PowerShell

2
  • 1
    Perfect! And following the second link, just changing the definition of $account in = $web.EnsureUser($groupName) shares the permission with AD groups, thank you very much!!
    – Mafalda89
    Apr 1, 2015 at 13:52
  • using $web.SiteGroups["Site Admins"] returns nothing for me. Also, using $web.Sitegroups returns an 'access is denied' error.
    – Jelphy
    Jul 17, 2018 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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