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.
- $adminUrl = "https://mycmpw-admin.sharepoint.com"
- $url = "https://mycmpw.sharepoint.com/sites/demosite"
- $username= "[email protected]"
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.
- $csvlocation = "C:\Users\GauravKK\Desktop\Emails\usertogrp.csv"
- $values = Import-Csv -Path $csvlocation
- 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
- try {
- Connect - SPOService - url $siteUrl - credential $username
- foreach($val in $values) {
- $groupname = $val.group
- $username = $val.user
- Add - SPOUser - Site $url - LoginName $username - Group $groupname
- Write - Host - ForegroundColor green $username "Added to group"
- $groupname "Successfully"
- }
- }
Snippet 3. Reading each user from csv and adding to group in loop
Catch block is as follows.
- catch {
- write - host "Error"
- $_.exception
- $errorlabel = $true
- Write "Error: "
- $_.Exception.Message
- }
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.
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.
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
- $adminUrl = "https://mycmpw-admin.sharepoint.com"
- $url = "https://mycmpw.sharepoint.com/sites/demosite"
- $username = "[email protected]"
- $csvlocation = "C:\Users\GauravKK\Desktop\Emails\usertogrp.csv"
- $values = Import - Csv - Path $csvlocation
- Write - Host - ForegroundColor green $values;
- try {
- Connect - SPOService - url $siteUrl - credential $username
- foreach($val in $values) {
- $groupname = $val.group
- $username = $val.user
- Add - SPOUser - Site $url - LoginName $username - Group $groupname
- Write - Host - ForegroundColor green $username "Added to group"
- $groupname "Successfully"
- }
- } catch {
- write - host "Error"
- $_.exception
- $errorlabel = $true
- Write "Error: "
- $_.Exception.Message
- }