Adding Users To SharePoint Group

This blog shows how to add multiple users to a SharePoint group using Powershell script.

Sometimes, we need to add a user to the SharePoint group to give site access to the user. If there are one or two users, it can be done through Site Settings->People and Groups, selecting a group and clicking a new user to add the user. But what if there are more than a hundred users? This process will take hours to finish. Thankfully, we can achieve this using PowerShell script to add multiple users to SharePoint group which reads from .csv (Comma Separated Value) file.

Let us see the PowerShell Script to accomplish the above task.

At the very first, provide admin URL, site URL, and username. See the following figure. 1.
  1. $adminUrl = "https://mycmpw-admin.sharepoint.com"  
  2. $url = "https://mycmpw.sharepoint.com/sites/demosite"  
  3. $username= "gaurav@mycmpw.onmicrosoft.com"  
Snippet 1. Provide site details
  • Then, provide the CSV file’s location as shown in figure. 2.
  • Import the CSV file from the path specified in “$csvlocation” as shown below. Write-host is nothing but a way of printing message on the output window.
    1. $csvlocation = "C:\Users\GauravKK\Desktop\Emails\usertogrp.csv"  
    2. $values = Import-Csv -Path $csvlocation  
    3. Write-Host -ForegroundColor green $values;  
Snippet 2. Provide CSV location

In try block, write a command for connecting to our SharePoint site and in the foreach loop, get each value one by one. In the following code, “$val.group” is to get group name where the user needs to be added. Similarly, “$val.user” is user's login name. These both values are stored in CSV file. And then, finally, write a command to add user to the group which is “Add-SPOUser”. See following Snippet.3
  1. try {  
  2.     Connect - SPOService - url $siteUrl - credential $username  
  3.     foreach($val in $values) {  
  4.         $groupname = $val.group  
  5.         $username = $val.user  
  6.         Add - SPOUser - Site $url - LoginName $username - Group $groupname  
  7.         Write - Host - ForegroundColor green $username "Added to group"  
  8.         $groupname "Successfully"  
  9.     }  
  10. }  

Snippet 3. Reading each user from csv and adding to group in loop

Catch block is as follows.

  1. catch {  
  2.     write - host "Error"  
  3.     $_.exception  
  4.     $errorlabel = $true  
  5.     Write "Error: "  
  6.     $_.Exception.Message  
  7. }  
Snippet 4. Catch Block

And finally, run the code. You will see the following window. Write your password in the textbox provided next to Password and click OK.

Credential window
Figure 5. Credential window

Let us see the output. The user-specified user will be added to the SharePoint group which was specified in the CSV file.

Output on Console
Figure 6. Output on Console

I had two users only on my site but the same is the code for adding multiple users.

That’s how PowerShell script can be used to add multiple users to a SharePoint group.

Full Code
  1. $adminUrl = "https://mycmpw-admin.sharepoint.com"  
  2. $url = "https://mycmpw.sharepoint.com/sites/demosite"  
  3. $username = "gaurav@mycmpw.onmicrosoft.com"  
  4. $csvlocation = "C:\Users\GauravKK\Desktop\Emails\usertogrp.csv"  
  5. $values = Import - Csv - Path $csvlocation  
  6. Write - Host - ForegroundColor green $values;  
  7. try {  
  8.     Connect - SPOService - url $siteUrl - credential $username  
  9.     foreach($val in $values) {  
  10.         $groupname = $val.group  
  11.         $username = $val.user  
  12.         Add - SPOUser - Site $url - LoginName $username - Group $groupname  
  13.         Write - Host - ForegroundColor green $username "Added to group"  
  14.         $groupname "Successfully"  
  15.     }  
  16. catch {  
  17.     write - host "Error"  
  18.     $_.exception  
  19.     $errorlabel = $true  
  20.     Write "Error: "  
  21.     $_.Exception.Message  
  22. }