SharePoint Online: Bulk Import Users to Groups from a CSV File using PowerShell

Requirement: Bulk add users from a CSV file to SharePoint Online Groups.

If you need to bulk add users to a group in SharePoint Online, PowerShell is the way to go. In this post, we’ll walk you through the steps needed to add users to a group using PowerShell. Here is the CSV file with users and groups data to bulk add users to groups:

sharepoint online bulk add users to group

You can download the CSV file here:

SharePoint Online: Bulk Add Users to Group using PowerShell

SharePoint Online groups help to manage security effectively by combining user accounts and security groups. Typically, Active Directory user and security groups will be added to SharePoint groups, and then SharePoint groups are used to assign permissions within sites. Here is my PowerShell script to import users from a CSV file to SharePoint Online groups:

#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"

Function Import-SPOUserFromCSV($CSVFile)
{  
    #Get data from CSV
    $UserData = Import-CSV $CSVFile

    #Get Credentials to connect
    $Cred = Get-Credential

    ForEach($Row in $UserData)
    {
        #Get Data from CSV
        $SiteURL = $Row.SiteURL
        $GroupName= $Row.GroupName
        $UserAccount = $Row.UserAccount
  
        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 Web and Group
            $Web = $Ctx.Web
            $Group= $Web.SiteGroups.GetByName($GroupName)
 
            #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' in Site '$SiteURL'"
        }
        Catch {
            write-host -f Red "Error Adding user to Group!" $_.Exception.Message
        }
    }
}

#Call the function
Import-SPOUserFromCSV "C:\Temp\UserData.csv"

PnP PowerShell to Bulk Add Users from CSV File

We can add users to groups in SharePoint Online from an Excel file or CSV using PowerShell. Here is how to add users in bulk with PnP PowerShell:

#Variables
$CSVPath  ="C:\temp\UserData.csv"

#Get data from CSV
$CSVData = Import-Csv $CSVPath

#Iterate through each row in CSV
ForEach($Row in $CSVData)
{
    Try { 
        #Connect to SharePoint Online Site
        Write-host "Connecting to Site: "$Row.SiteURL
        Connect-PnPOnline -Url $Row.SiteURL -Interactive
 
        #Get the group
        $Group = Get-PnPGroup -Identity $Row.GroupName
 
        #Add Each user to the Group
        Add-PnPGroupMember -LoginName $Row.UserAccount -Identity $Group
        Write-host -f Green "`tAdded User $($Row.UserAccount) to $($Group.Title)"
    }
    Catch {
        write-host -f Red "Error Adding User to Group:" $_.Exception.Message
    }
}

This can be helpful when you’re setting up a new team site, and need to quickly add all the members. Or maybe you just want to add some new people to an existing group. Here is another post for SharePoint On-Premises to version: Import users from CSV File to SharePoint using PowerShell

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

9 thoughts on “SharePoint Online: Bulk Import Users to Groups from a CSV File using PowerShell

  • SharePoint Online: Bulk Add Users to Group using PowerShell

    I am getting the error – “Error Adding user to Group! Exception calling “ExecuteQuery” with “0” argument(s): “The remote server returned an error: (404) Not Found.”

    The script is able to add only one row in from the CSV, for the row function, it is giving the above error. Please help me with the missing piece.

    Reply
    • Verify the “SiteURL” column values in the CSV. Make sure they are valid and doesn’t include any “Space” characters at the end.

      Reply
  • Use the -Interactive command to avoid the authentication prompt

    Reply
  • I couldnt get this to work, kept error out for me

    Reply
  • Hello, I get the following error when I run the first program
    Error Adding user to Group! A constructor was not found. Cannot find an appropriate constructor for type Microsoft.SharePoint.Client.ClientContext.

    Reply
  • Thank you for this! Does this script send an email invite to users? I need to be able to add bulk users without sending them an email invite. Thank you!

    Reply
    • Hello. I know you posted this message several months ago. I’ve just tested the PNP script and it didn’t email users.

      Reply
  • Thank you for this. The PnP PowerShell worked very well with some modifications I made for my environment. I changed the Connect-PnPOnline to -Interactive and removed the DisConnect-PnPOnline as it was prompting for authentication after each user. I had around 500 users so that was very cumbersome.

    Reply
    • Yes! You can remove the DisConnect-PnPOnline as the Connect-PnPOnline will cache the authentication from the previous connection without prompting.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *