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
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
-
1Perfect! 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!! 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.– JelphyJul 17, 2018 at 11:34