Add members to office 365 SecurityGroup using PowerShell and CSV



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.
Note – Sign In address need to be added under the userPrincipleName.

Step 2.  Run The below command.
$sub = Import-Csv C:\RAhul\sspruser.com.csv csv   {enter the Path of same/Step1 CSV that was created by you with users details}

$sub | Foreach {Get-Msoluser -UserPrincipalName $_.Userprincipalname | select Objectid } | Export-csv C:\RAhul\sspruser.com.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.
The below command will help with the object ID of the Group.
Get-MsolGroup -all | where-object { $_.DisplayName -eq "SSPRSecurityGroupUsers"} | 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
$sub2 | Foreach {Add-MsolGroupMember -groupObjectid ‘XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX’ -GroupMemberObjectId $_.ObjectId -GroupMemberType User}


Step 5. Extract the users from the Group.

Get-MsolGroupMember -all -groupObjectid 'XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX' | Select DisplayName,EmailAddress,GroupMemberType | Export-csv C:\RAhul\security-group-members.csv

Comments

Post a Comment

Popular Posts