Home

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

%3CLINGO-SUB%20id%3D%22lingo-sub-211842%22%20slang%3D%22en-US%22%3EAdd%20Bulk%20users%20to%20SharePoint%20Online%20Groups%20from%20.csv%20using%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-211842%22%20slang%3D%22en-US%22%3E%3CP%3EPowershell%20script%20Add%20bulk%20users%20from%20.csv%20file%20to%20SharePoint%20online%20group.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%23Load%20SharePoint%20CSOM%20Assemblies%3CBR%20%2F%3EAdd-Type%20-Path%20%22C%3A%5CProgram%20Files%5CCommon%20Files%5CMicrosoft%20Shared%5CWeb%20Server%20Extensions%5C16%5CISAPI%5CMicrosoft.SharePoint.Client.dll%22%3CBR%20%2F%3EAdd-Type%20-Path%20%22C%3A%5CProgram%20Files%5CCommon%20Files%5CMicrosoft%20Shared%5CWeb%20Server%20Extensions%5C16%5CISAPI%5CMicrosoft.SharePoint.Client.Runtime.dll%22%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%23Variables%20for%20Processing%3CBR%20%2F%3E%24SiteURL%20%3D%20%22https%3A%2F%2F%22%3CBR%20%2F%3E%23%24UserAccount%20%3D%20%22123%40sp.com%22%3CBR%20%2F%3E%24GroupName%3D%22Visitors%22%3C%2FP%3E%3CP%3E%24CSVFile%3D%22%5CAct.csv%22%3C%2FP%3E%3CP%3E%24UserAccount%20%3D%20Import-Csv%20%24CSVFile%20%7C%20ForEach-Object%20%7B%20%24_.UserAccount%7D%3CBR%20%2F%3E%26nbsp%3B%3CBR%20%2F%3E%23Setup%20Credentials%20to%20connect%3CBR%20%2F%3E%24Cred%20%3D%20Get-Credential%3CBR%20%2F%3E%24Cred%20%3D%20New-Object%20Microsoft.SharePoint.Client.SharePointOnlineCredentials(%24Cred.UserName%2C%24Cred.Password)%3CBR%20%2F%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETry%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%23Setup%20the%20context%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24Ctx%20%3D%20New-Object%20Microsoft.SharePoint.Client.ClientContext(%24SiteURL)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24Ctx.Credentials%20%3D%20%24Cred%3CBR%20%2F%3E%26nbsp%3B%3CBR%20%2F%3E%24Web%20%3D%20%24Ctx.Web%3CBR%20%2F%3E%24Group%3D%20%24Web.SiteGroups.GetByName(%24GroupName)%3C%2FP%3E%3CP%3Eforeach(%24user_single%20in%20%24UserAccount)%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%24User%3D%24web.EnsureUser(%24user_single)%3CBR%20%2F%3E%26nbsp%3B%20%24Ctx.Load(%24User)%3B%3CBR%20%2F%3E%24Ctx.ExecuteQuery()%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%23Add%20user%20to%20the%20group%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24Result%20%3D%20%24Group.Users.AddUser(%24User)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24Ctx.ExecuteQuery()%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bwrite-host%26nbsp%3B%20-f%20Green%20%22User%20'%24UserAccount'%20has%20been%20added%20to%20'%24GroupName'%22%3CBR%20%2F%3E%7D%3CBR%20%2F%3ECatch%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bwrite-host%20-f%20Red%20%22Error%20Adding%20user%20to%20Group!%22%20%24_.Exception.Message%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHappy%20SharePointing%20!%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-211842%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
Kamal Pandey
Contributor

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 = "123@sp.com"
$GroupName="Visitors"

$CSVFile="\Act.csv"

$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){
  
$User=$web.EnsureUser($user_single)
  $Ctx.Load($User);
$Ctx.ExecuteQuery();
     #Add user to the group
     $Result = $Group.Users.AddUser($User)
     $Ctx.ExecuteQuery()

}
   
    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 ! 

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
30 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies