SharePoint Online: Add User to Group using PowerShell
Requirement: SharePoint Online PowerShell to Add User to Group.
How to add users to an existing group in SharePoint Online?
Each SharePoint Online site comes with a default set of SharePoint groups, such as Owners, Members, and Visitors. You can add users to these groups so that you can manage group permissions in a single place instead of managing each individual user. Creating SharePoint groups with custom permission levels is also possible. In this guide, we will show you how to add a user to a group in SharePoint Online, and we’ll also provide you with how to add a user to a group in SharePoint Online using PowerShell.
- Login to SharePoint Online site >> Click on Site Settings >> Under the Site Settings page, in the Users And Permissions section, click on the “Site Permissions” link.
- On the Permissions page, click on the “Grant Permissions” button in the ribbon from the Permissions tab.
- In the Share dialog box, enter name/email addresses
- Click on the “Hide Options” link button. In the Select A Group Or Permission Level list box, select the appropriate group name, such as “Members”.
- Click on the “Share” button.
Alternatively, You can Go to the Site Settings page >> Click on the “People and Groups” link under the “Users and Permissions” section. On the People and Groups page, click on the group’s name from the Groups list in the left navigation. On the Group’s page, click on the arrow next to the “New” link and Add Users.
SharePoint Online PowerShell to Add User to Group
Here is how we can add a user to the group using PowerShell in SharePoint Online:
#Load SharePoint CSOM 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"
#Variables for Processing
$SiteURL = "https://crescent.sharepoint.com/Sites/Sales"
$UserAccount = "[email protected]"
$GroupName="Sales Members"
#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 Group
$Web = $Ctx.Web
$Group= $Web.SiteGroups.GetByName($GroupName)
#ensure user sharepoint online powershell - Resolve the User
$User=$web.EnsureUser($UserAccount)
#Add user to the group
$Result = $Group.Users.AddUser($User)
$Ctx.Load($Result)
$Ctx.ExecuteQuery()
write-host -f Green "User '$UserAccount' has been added to '$GroupName'"
}
Catch {
write-host -f Red "Error Adding user to Group!" $_.Exception.Message
}
You can also use Add-SPOUser using SharePoint Online management shell: SharePoint Online: PowerShell to Add Users to Group
SharePoint Online: PowerShell to Add User to Owner Group
Here is how to add a User to the associated owner group of the site.
#Load SharePoint CSOM 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"
#Variables for Processing
$SiteURL = "https://crescent.sharepoint.com/Sites/marketing"
$UserAccount = "[email protected]"
#Setup Credentials to connect
$Cred = Get-Credential
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Get the Default Owner Group of the Web
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.Load($Web.AssociatedOwnerGroup)
$Ctx.ExecuteQuery()
#ensure the sharepoint online user
$User=$web.EnsureUser($UserAccount)
#sharepoint online powershell add user to owner group
$Web.AssociatedOwnerGroup.Users.AddUser($User) | Out-null
$Ctx.ExecuteQuery()
write-host -f Green "User '$UserAccount' has been added to Owners Group of the Site!"
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
SharePoint Online: Add user to Group using PnP PowerShell
Let me show you how to use PnP PowerShell to add users to a group in SharePoint Online:
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/Sales"
$GroupName="Sales Portal Members"
$UserLoginID = "[email protected]"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#sharepoint online powershell to add user to group
Add-PnPGroupMember -LoginName $UserLoginID -Identity $GroupName
Similarly, if you want to add a user to the default members group of the site, use the following script:
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$UserAccount= "[email protected]"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get the Default Members Group
$MembersGroup = Get-PnPGroup -AssociatedMemberGroup
#sharepoint online pnp powershell add user to group
Add-PnPGroupMember -LoginName $UserAccount -Identity $MembersGroup
Can I add a user to multiple Sites? Sure, why not!
#Define Sites List
$Sites = "https://crescent.sharepoint.com/sites/Sales",
"https://crescent.sharepoint.com/sites/Marketing",
"https://crescent.sharepoint.com/sites/Purchase",
"https://crescent.sharepoint.com/sites/Operations"
#Loop Through Each Site
ForEach ($URL in $Sites)
{
#Connect to Site
Connect-PnPOnline -Url $URL -Interactive
#Get the Default Members Group
$MembersGroup = Get-PnPGroup -AssociatedMemberGroup
#Add User to Group
Add-PnPGroupMember -Identity $MembersGroup -EmailAddress "[email protected]"
Write-host "Added User to Site:"$URL
}
To add users from a CSV file to SharePoint Online groups, use: SharePoint Online: Bulk Add Users to Groups from a CSV File using PowerShell
Is there anyway to add M365 groups to Sharepoint Groups using this Cmdlet ? I’ve tried all the following
Add-PnPGroupMember -LoginName “[email protected]” -Group ‘Sharepoint Site Visitors’
Add-PnPGroupMember -LoginName “M365 Group Members” -Group ‘Sharepoint Site Visitors’
Add-PnPGroupMember -LoginName “M365 Group” -Group ‘Sharepoint Site Visitors’
Add-PnPGroupMember -LoginName “3cd69402-d332-4b52-11r4-f123cdf987v5” -Group ‘Sharepoint Site Visitors’
But they all fail
Add-PnPGroupMember : The specified user could not be found
How to send email invitations while adding users through PowerShell?
Thanks so much for your help. This save me alot of time.