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.

Here is the CSV file with users and groups data to bulk add users to groups:

sharepoint online bulk add users to group

SharePoint Online: Bulk Add Users to Group using PowerShell

SharePoint Online groups helps 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\users.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)"
        DisConnect-PnPOnline
    }
    Catch {
        write-host -f Red "Error Adding User to Group:" $_.Exception.Message
    }
}

Here is another post for SharePoint On-Premises to version: Import users from CSV File to SharePoint using PowerShell

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

6 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
  • 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
  • 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

Leave a Reply