Forum Discussion

Kamal Pandey's avatar
Kamal Pandey
Copper Contributor
Jul 03, 2018

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 = "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 ! 

No RepliesBe the first to reply

Resources