SharePoint Online: PowerShell to Add Group Members
Requirement: Add a user to a group in SharePoint Online using PowerShell.
How to Add Members to a SharePoint Online Group?
One of the most common tasks that SharePoint administrators need to do is add new members to groups. This post will cover how to add users to groups in SharePoint Online. To provide access to your SharePoint Online site, you can add users to SharePoint Groups such as the default groups: Site Owners, Site Members, or Site Visitors. How do I add a user to a group in SharePoint? Here is how to add members to the SharePoint group:
- In the browser, Log in to the SharePoint Online site to which you wish to add members as an administrator.
- On the site’s home page, click the Settings gear icon >> Choose Site permissions >> and then click on “Advanced Permissions Settings”. Alternatively, You can click on Settings >> Site Settings >> “Users and Permissions” link under “People and Groups” from anywhere else on the site.
- A list of SharePoint groups appears. Click on the name of the group >> Once the desired group has been chosen, click on the “New” button in the toolbar and then choose “Add Users” to add users to the group.
- In the Add users window, Enter the user’s email address or username and click on the “Share” button.
The user will now be added to the specified group of your SharePoint site!
SharePoint Online: Add User to Group using PowerShell
Let’s see how to use PowerShell to add a user to a group in SharePoint Online. This can be helpful if you need to automate the process of adding users to groups, or if you just want to save yourself some time by not having to add users one by one manually.
To add an individual user to the SharePoint Online site collection group using PowerShell, use the Add-SPOUser cmdlet from the SharePoint Online management shell. Here is a step-by-step guide on how to add users to a SharePoint Online group via PowerShell:
Step 1: Enter the connection Credentials for SharePoint Online Admin Center
$Credential = Get-credential
This prompts for the user name and password. Key in your SharePoint Online Administrator credentials.
Step 2: Connect to SharePoint Online Service:
To connect with SharePoint Online from PowerShell, run this Windows PowerShell cmdlet. Make sure you change the URL parameter to your SharePoint Admin Center URL.
Connect-SPOService -url https://Crescent-admin.sharepoint.com -Credential $Credential
Step 3: Add User to SharePoint Online Site Group:
Once the connection is established successfully in the previous step, you can add a user to a group.
Add-SPOUser -Site "https://Crescent.sharepoint.com/sites/Sales" -Group "Sales Members" -LoginName salaudeen@Crescent.com
The SharePoint Online Management Shell looks like this:
You can also use CSOM PowerShell to add users to a group. Here is how: SharePoint Online: Add user to a group using PowerShell CSOM
Bulk Add Users from a CSV File to SharePoint Online Group using PowerShell
Now, the next question is, What if you want to add hundreds of users to a site collection? Well, adding multiple users to a SharePoint Online group is easy with PowerShell. In just a few lines of code, you can add as many users as you need and have them quickly added to the group to save time. Here is how to add multiple users to groups in SharePoint Online from a CSV file.
Step 1: Create a CSV file and fill it in according to your requirements.
You can download the CSV template here:
Step 2: Use this PowerShell script to Read from CSV and import bulk users to SharePoint Group:
#Connection paramaters
$AdminURL = "https://Crescent-admin.sharepoint.com"
$AdminName = "SPAdmin@Crescent.com"
$CSVFile="C:\UsersToAdd.csv"
#User Names Password to connect
$Password = Read-host -assecurestring "Enter Password for $AdminName"
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Password
#Connect to SharePoint Online
Connect-SPOService -url $AdminURL -credential $Credential
#add multiple users to sharepoint online group powershell - Import Users from CSV
Import-Csv $CSVFile | ForEach-Object { Add-SPOUser -Site $_.SiteURL -Group $_.GroupName -LoginName $_.UserAccount}
To import users to group in bulk from an Excel file, use: Add users to group in SharePoint Online using PowerShell from Excel
PnP PowerShell to Bulk Add Users to Multiple Site Groups in SharePoint Online
Here is another case: How do I add multiple users to multiple site collections Owners’ groups? The PnP PowerShell module makes it easy to add multiple users to a SharePoint Online group.
#Variables
$SiteURLs = "Sales","Intranet","Marketing"
$UserIDs = "Salaudeen@crescent.com","Steve@crescent.com"
#Iterate through each site
ForEach($Site in $SiteURLs)
{
#Connect to SharePoint Online Site
Write-host "Connecting to Site: "$Site
Connect-PnPOnline -Url https://crescent.sharepoint.com/sites/$Site -Interactive
#Get the Associcated Owners group of the site
$Web = Get-PnPWeb
$Group = Get-PnPGroup -AssociatedOwnerGroup
#sharepoint online add user to group powershell
ForEach($User in $UserIDs)
{
Add-PnPGroupMember -LoginName $User -Identity $Group
Write-host -f Green "`tAdded $User to $($Group.Title)"
}
}
To add an external user to a group in SharePoint Online PowerShell, use: How to Add External User to SharePoint Online Group using PowerShell?
To add a user as a collection administrator with PowerShell, use: “Set-SPOUser” cmdlet as: Set-SPOUser -site “Site-URL” -LoginName “User-Login-ID” -IsSiteCollectionAdmin $True. You can also use the PnP PowerShell cmdlet “Add-PnPSiteCollectionAdmin”. Please note, You have to add site collection admin to all sites in the tenant. Global Admin doesn’t get access to SharePoint sites automatically.
More info: How to add a site collection administrator in SharePoint Online?
To remove users from SharePoint Group, navigate to your SharePoint Online site >> Click Settings >> Site Permissions >> Advanced Permissions Settings. Select the group from which you’d like to remove the users >> Select the user(s) you want to remove >> Click the Actions drop-down arrow >> Select Remove Users from Group. Use the “Remove-SPOUser” PowerShell cmdlet if you would like to do it with PowerShell. CSOM and PnP PowerShell scripts can also be used.
More info: Remove user from SharePoint online group using PowerShell
If you need to export SharePoint Online users and groups to excel, use this PowerShell script: Export SharePoint Online users and groups to excel using PowerShell
Is there a way to combine adding multiple users to multiple groups for multiple subsites under a site collection? I have a primary site collection that has multiple sites, each with their own “Members” groups and I want to bulk load users to each group for each subsite using the CSV method.