Thursday 5 December 2019

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

https://techcommunity.microsoft.com/t5/Microsoft-365/Add-Bulk-users-to-SharePoint-Online-Groups-from-csv-using/m-p/211842


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
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Blog Archive