Add Bulk users to SharePoint Online Groups from .csv using PowerShell


Powershell script Add bulk users from .csv file to SharePoint online group. 


#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"
#Variables for Processing
$SiteURL = "https://"
#$UserAccount = ""


$UserAccount = Import-Csv $CSVFile | ForEach-Object { $_.UserAccount}
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred
$Web = $Ctx.Web
$Group= $Web.SiteGroups.GetByName($GroupName)

foreach($user_single in $UserAccount){
     #Add user to the group
     $Result = $Group.Users.AddUser($User)

    write-host  -f Green "User '$UserAccount' has been added to '$GroupName'"
Catch {
    write-host -f Red "Error Adding user to Group!" $_.Exception.Message


Happy SharePointing ! 

0 Replies