Add AD Group to SharePoint Online Group using PowerShell

Requirement: Add AD Group to SharePoint Online Group using PowerShell

PowerShell to Add Active Directory Group to SharePoint Online Group

PowerShell to Add Active Directory Group to SharePoint Online Group

#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Add-ADGroupToSP($SiteURL,$ADGroupName,$SPGroupName)
{
    #Setup Credentials to connect
    $Cred = Get-Credential
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred
     
        #Get the Web and SharePoint Group
        $Web = $Ctx.Web
        $Group= $Web.SiteGroups.GetByName($SPGroupName)
 
        #Resolve the AD Security Group
        $ADGroup = $web.EnsureUser($ADGroupName)
 
        #sharepoint online powershell add AD group to sharepoint group
        $Result = $Group.Users.AddUser($ADGroup)
        $Ctx.Load($Result)
        $Ctx.ExecuteQuery()
 
        write-host  -f Green "Active Directory Group '$ADGroupName' has been added to '$SPGroupName'"
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Variables for Processing
$SiteURL = "https://crescenttech.sharepoint.com/Sales"
$ADGroupName = "Marketing Managers"
$SPGroupName="Sales Portal Members"

#Call the function to add AD group to SharePoint Group
Add-ADGroupToSP -SiteURL $SiteURL -ADGroupName $ADGroupName -SPGroupName $SPGroupName

This adds given Active directory security group into SharePoint Online group as a SharePoint user. You can also use AD Group’s Login ID (E.g. c:0t.c|tenant|915xnusf-fbb3-7da1-k252-33e0de69f19″) to Add AD Group to SharePoint Online Group.

SharePoint Online: PnP PowerShell to Add Security Group to Site

Here is the PnP PowerShell to add Active directory security group to SharePoint Online group:

#Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/hr"
$AdGroupID = "c:0t.c|tenant|798cb3d4-7ca8-4567-adb5-916bc496d7cd"
$GroupName = "HR Owners"

#Connect to site
Connect-PnPOnline $SiteURL -UseWebLogin

#Sharepoint online powershell add security group
Add-PnPUserToGroup -LoginName $AdGroupID -Identity $GroupName

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

8 thoughts on “Add AD Group to SharePoint Online Group using PowerShell

  • May 5, 2021 at 4:05 PM

    The code worked like a charm!! excellent. keep posting such scenarios. thanks 🙂

    Reply
  • February 1, 2021 at 12:45 PM

    Excellent, thanks for sharing!

    Reply
  • November 24, 2020 at 3:40 AM

    Hey Salaudeen,

    Just wanted to say I’ve used your blog posts quite a lot for my current project. Great work!

    Reply
  • April 19, 2020 at 4:26 PM

    Need help with this code, I don’t know how to start this in my sharepoint 2016 on-premise – site colletion

    Reply
  • April 19, 2020 at 4:17 PM

    Could you explane please. I don’t now how this code work in sharepoint on-premise ??

    When I star your code is for sharepoint ONLINE and I get error
    ew-Object : Cannot find type [Microsoft.SharePoint.Client.SharePointOnlineCredentials]: verify that the assembly conta
    ining this type is loaded.

    Reply
  • August 23, 2019 at 1:34 PM

    What is the same code for PNP Powershell?

    Reply
    • November 25, 2019 at 7:51 AM

      $context = Connect-PnPonline -Url [mysite] -ReturnConnection
      $web = Get-PnPWeb -Connection $context
      $adgroup = “c:0t.c|tenant|[ad id]”
      $ensureUser = $web.EnsureUser($adgroup)
      $adgroup= Get-PnPUser $adgroup -Connection $context
      Add-PnPUserToGroup -LoginName $adgroup.LoginName -Identity “$($web.Title) Owners” -Connection $context

      Reply

Leave a Reply