
How to Add Bulk Users to SharePoint Group using PowerShell
SharePoint groups allow members to access SharePoint content, such as documents, lists, and libraries. As a SharePoint admin often, you might be in the situation of adding multiple users to a SharePoint group. This blog will help you manage SharePoint Online group membership using PowerShell.
Before running the script/cmdlet, you must connect to SharePoint Online PowerShell.
Content:
- Add bulk users to the SPO group
- List all the SharePoint Online groups
- Get SharePoint Online group members
- Remove bulk users from the SPO group
Add Bulk Users to SharePoint Group using PowerShell:
To bulk import users from the CSV file and add those users to SharePoint Online group, you can use the following PowerShell script.
Connect-SPOService Import-CSV <FilePath> | foreach { $UPN=$_.UPN Write-Progress -Activity "Adding $UPN to SharePoint group..." Add-SPOUser –Site <SiteUrl> -Group <GroupName> -LoginName $UPN If($?) { Write-Host $UPN Successfully added -ForegroundColor Green } Else { Write-Host $UPN - Error occurred –ForegroundColor Red } }
In the above script, you can replace the following.
<FilePath> –>CSV file path.
<SiteUrl> –> Site URL (You can use the Get-SPOSite cmdlet to get the site url).
<GroupName> -SharePoint group name in which you want to add members.
The input CSV should follow the format below.
How to List SharePoint Online Groups?
To list all the groups available in the specific site, use the ‘Get-SPOSiteGroup’ cmdlet.
Get-SPOSiteGroup –Site <SiteURL>
To view all the groups in SharePoint Online, run the following code snippet.
Get-SPOSite –Limit All | foreach { Write-Host Site: $_.url -Foregroundcolor Yellow Get-SPOSiteGroup –Site $_.url | Select LoginName }
The output lists all the sites and groups available on those sites.
How to Get SharePoint Online Group Members?
To view SharePoint Online group members, you can run the ‘Get-SPOUser’ cmdlet with site URL and the respective group name.
Get-SPOUser –Site <SiteURL> -Group <GroupName>
To view all the groups in a specific site and their members, execute the below code.
Get-SPOSiteGroup -Site <SiteURL> | foreach {
Write-Host Group name: $_.LoginName -ForegroundColor Yellow
Get-SPOUser -Site <SiteURL> -Group $_.LoginName | select LoginName
}
Remove Bulk Users from SharePoint Online Group:
After generating the SharePoint Online group membership report, admins can check for unnecessary permission grants. If required, admins can remove those members from the SharePoint Online group. You can use the following PowerShell script to bulk import users from the CSV file and remove those members from the SPO group.
Connect-SPOService Import-CSV <FilePath> | foreach { $UPN=$_.UPN Write-Progress -Activity "Removing member $UPN from SharePoint group..." Remove-SPOUser –Site <SiteUrl> -Group <GroupName> -LoginName $UPN If($?) { Write-Host $UPN Successfully removed -ForegroundColor Green } Else { Write-Host $UPN - Error occurred –ForegroundColor Red } }
I hope this blog will help admins add and remove multiple members to SharePoint site groups and know about SPO group membership detail.