Add multiple users to Office 365 security groups using PowerShell Scripts

Step1:  Connect MSOL Services

Connect-MsolService

Step2: Find out ObjectID of the Security Group you would like add members to

Get-MsolGroup –Maxresults 100000 | Where-Object {$_.DisplayName -eq “Test Security Group”}

Get-MsolGroup –ObjectId “af407072-7ae1-4b07-a0ca-6634b7396054”

OR

Sign-in to Portal.Azure.Com and Select Azure Active Directory>Security Groups>Search the Group>Go to properties of the group and copy the ObjectID

Step3: Create a CSV file with a header UserPrincipalName and list all email addresses in one column of CSV file e.g.

UserprincipalName

Test1@domain.com

Test2@domain.com

Step4: Execute the PowerShell Script to add users into the Security group

Copy the script and paste it in a notepad. Rename the notepad to Add-MsolGroupMembers.PS1

Import-CSv -Path “c:\Temp\testscript.CSV” | ForEach {

$UPN=$_.UserPrincipalName

$Users=Get-MsolUser -UserPrincipalName $UPN

$Groupid = Get-MsolGroup -ObjectId “0c3c9f82-2392-43cc-bc00-b0d7b5734ac4”

$Users | ForEach {Add-MsolGroupMember -GroupObjectId $GroupID.ObjectID -GroupMemberObjectId $Users.ObjectID -GroupMemberType User}

}

Run the scripts