Step 1. Create a CSV file with a column “UserPrincipalName” and add all users under it who are to be added as a member of the group.
Step 2. Run The below command to import the csv file and get the object IDs for members to be added to group
Import-Csv C:\temp\Members.csv csv | Foreach {Get-Msoluser -UserPrincipalName $_.Userprincipalname | select Objectid } | Export-csv C:\temp\MembersWithObjectID.csv
This will convert the user’s identity to their unique guid details, and export it to the same CSV file.
Step 3. Collect the guid ID of the security group as well to which you want to add the mebers
The below command will help with the object ID of the Group.
Get-MsolGroup “SecurityGroupName” | FL
I have my object ID as below.
ObjectId : XXXXXX-XXXX-XXXX-XXXXXXXXX
Step 4. Run the below command to Add members in the CSV to the Group.
$sub2 = Import-Csv C:\RAhul\sspruser.com.csv
Import-Csv C:\temp\MembersWithObjectID.csv | Foreach {Add-MsolGroupMember -groupObjectid ‘XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX’ -GroupMemberObjectId $_.ObjectId -GroupMemberType User}
Step 5. Verify the users from the Group just added.
Get-MsolGroupMember -all -groupObjectid ‘XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX’ | Select DisplayName,EmailAddress,GroupMemberType | Export-csv C:\temp\security-group-members.csv