Add Active Directory Group to SharePoint using PowerShell

Requirement: Add Active Directory Group to SharePoint Group using PowerShell

How to Add Active Directory Group to SharePoint?
To add AD group to SharePoint, do the following:
  • Navigate to your SharePoint site and click on Site Settings gear icon >> Select "Site settings"
  • Click on "People and groups" link under "Users and Permissions"
  • Now you can either provide direct permissions by clicking on the "Grant Permissions" icon in the ribbon or add Active Directory group to any existing SharePoint group by selecting existing groups. 
  • Click on "New" button >> and select "Add Users". Enter the full name of the AD group to add. Make sure all your entries are resolved. Click on the "Share" button to complete adding Active directory group to the SharePoint group.
    sharepoint add active directory group to sharepoint group

PowerShell to Add Active Directory Group to SharePoint:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Parameters
$SiteURL = "http://intranet.crescent.com"
$ADGroupName = "Crescent\Marketing Managers"
$PermissionLevel = "Edit"

Try {
    #Get Objects
    $Web = Get-SPWeb $SiteURL
    $ADGroup = $Web.EnsureUser($ADGroupName)
 
    #Grant Permission to the AD Group
    $RoleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($ADGroup)
    $RoleDefinition = $Web.RoleDefinitions[$PermissionLevel]
    $RoleAssignment.RoleDefinitionBindings.Add($RoleDefinition)
    $Web.RoleAssignments.Add($RoleAssignment)
    Write-host "Granted Edit Access to AD Group!" -f Green 
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

Add Active Directory Group to SharePoint Group using PowerShell:
Instead of providing direct user permissions to AD group, let's add the Active Directory group to an existing SharePoint Group.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$SiteURL = "http://Intranet.crescent/us/"
$ADGroupName = "Crescent\Marketing Managers"
$SharePointGroupName = "Crescent Intranet Members"

#Get Objects
$Web = Get-SPWeb $SiteURL
$ADGroup = $Web.EnsureUser($ADGroupName)
$SPGroup= $web.Groups[$SharePointGroupName]
 
#Add User to the Group
$SPGroup.AddUser($ADGroup)

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.