Adding multiple users to an Office365 Group
14/05/18 09:15 Filed in: Office365
How do you add multiple users to an Office365 group using PowerShell?
====
I was recently trying to set up some security groups in Office365, and I wanted to add a subset of users to that group. Could I work out how to do it? Well, yes, I could….But it took a while.
I tried the obvious approach of feeding each user to the add-msolgroupuser cmdlet and it just wasn't having it. Some further google-fu and I worked out that this command doesn't accept collections….So a different approach was needed.
In effect you create a variable with the users you want, and then feed that list of users to the add-msolgroupuser cmdlet. This worked for me anyway - so let's have a look at how below. I was working with a group for SharePoint in this instance.
Add your group to a variable
$group = get-msolgroup | where {$_.Displayname -eq 'SharePoint Users'}
Add your users to a variable
There's various ways to get your users. In my case it was simply users with a certain domain name so I selected those using:
$users=get-msoluser -domainname contoso.com|select-object userprincipalname,objectID
Add the users in the variable to your group
This is the bit I was struggling with originally. In effect you pipe the content of the users variable to individual add-msolgroupmember commands.
$users | foreach {add-msolgroupmember -groupobjectid $group.objectid -groupmembertype 'user' -GroupMemberObjectId $_.objectid}
Not as obvious as you'd imagine. Well, as I'd imagine anyway.
You can have a look at the group members with:
get-msolgroupmember -GroupObjectId $group.objectid
blog comments powered by Disqus