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 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
            #Add user to the group
            $Result = $Group.Users.AddUser($User)
            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 group in SharePoint Online PowerShell from Excel file or CSV. Here is how to add users in bulk with PnP PowerShell.

$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)"
    Catch {
        write-host -f Red "Error Adding User to Group:" $_.Exception.Message

Here is my 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!

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

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


Leave a Reply