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

About Raihan Al-Beruni

My Name is Raihan Al-Beruni. I am working as an Infrastructure Architect in Data Center Technologies in Perth, Western Australia. I have been working on Microsoft technologies for more than 15 years. Other than Microsoft technologies I also work on Citrix validated solution and VMware data center virtualization technologies. I have a Masters degree in E-Commerce. I am certified in Microsoft, VMware, ITIL and EMC. My core focus is on cloud technologies. In my blog I share my knowledge and experience to enrich information technology community as a whole. I hope my contribution through this blog will help someone who wants more information on data center technologies.
Gallery | This entry was posted in Office 365 and tagged , , , , , , , . Bookmark the permalink.

4 Responses to Add multiple users to Office 365 security groups using PowerShell Scripts

  1. Ashfaq says:

    Very nice.

    Like

  2. Jorge says:

    HI.
    I have been trying to add member in a group in this way
    $dcli = Get-MsolUser -All -DomainName “clinicaunisabana.edu.co” -Title “B”
    $dcliid = Get-MsolGroup -All | Where-Object {$_.DisplayName -eq “Deshabilitados Clinica”}
    $dcli | foreach {Add-MsolGroupMember -GroupObjectId $dcliid.ObjectId -GroupMemberObjectId $_.ObjectId -GroupMemberType User}

    But the result have been:
    Add-MsolGroupMember : You cannot update mail-enabled groups using this cmdlet. Use Exchange Online to perform this operation.

    For me it have been impossible to do, please help me

    Like

    • It appears that you are not adding members to MSolGroup. I have no visibility over your environment. You need to modify the script to suite your requirement.

      Like

      • jorge says:

        Hi Raihan, thank for you answer, but I don’t understand you, I have been doig this applying several solutions located in the web but the result is always the same.
        I like explain you acroos the web meeting, is it possible?

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s