Office 365, PowerShell

Adding a user to a group in office 365 using PowerShell

This article will walk you through how to add your Office 365 Users to an Office 365 Group.

There are a few pre-requisites to do this. One of which is having ‘Windows Azure Active Directory Module’ installed on your machine. Second, that you are an Administrator.

Scenario:

I have 10 users who I would like each to be added to an Office 365 Group. I’m a little too lazy to add one by one so I’ll look to do this with Powershell.

So first up I’ll need to create the group to add my 10 test users into.

From the Office 365 admin centre click Groups, click the + for this demo. I’ve created a group called  ‘Test Security Group’ and as you can see from the screenshot below no users have been added.

EmptySecurityGroup

 

Below is a screenshot of the users I will be adding to the security group.

testAccounts

 

 

 

 

 

 

 

 

 

OK so now we have our users & group we need to open up the Windows Azure Active Directory Module for Windows PowerShell

Once the powershell window is opened you will need to connect to your tenant. To do this type-

Connect-MsolService

You will then be prompted to type in a tenant administrators username/password combination to proceed.

Once authenticated we need to get the ID of the group. Easy.

$groupid = Get-MsolGroup | Where-Object {$_.DisplayName -eq “Test Security Group”}

This line is finding a group that matches the Group Display Name of “Test Security Group” and putting it into the $groupid variable.

We can now leave the group for now. It’s time to start getting the users id’s in order to add to this group.
GroupID

As I have a large number of users in my directory I conveniently named the users test1 to test10. Because of this I will get these users and put them to a variable so I can extract the information needed to add these users to a group.

$users = Get-MsolUser | Where-Object {$_.DisplayName -like ‘*Test*’}
UserVariable
It’s important to note that I am only filtering the users as I don’t want to add all users in the domain to this Active Directory. You can add and filter as you please.

It’s now time to add the users to the group. We are going to use the $users variable and run a foreach loop which will iterate through each user and grab it’s ObjectId and add to the security group.

$users | foreach {Add-MsolGroupMember -GroupObjectId $groupid.ObjectId -GroupMemberObjectId $_.ObjectId -GroupMemberType User} AddingUser

Depending on how many users you’re adding in bulk it may take a couple of minutes so be patient. Once it run’s, check back to your group and see if the users are added.

Added

 

You can download the full script here

Advertisements

One thought on “Adding a user to a group in office 365 using PowerShell

  1. James, many thanks for putting the effort in to share this. It is dedicated people such as yourself who make the internet as useful and valuable as it is. Thanks!

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