/ Powershell

Getting all members from an O365 Group with Powershell

Today a friend contacted med and wanted help with a Powershell one-liner. He wanted to get a list of all members in an Office 365 Group and find out whether or not the user was an OnPremise mailbox, Cloud mailbox or just a contact.

First of all we need to connect our Powershell session to Office 365 (or more specifically Exchange Online). See my previous post (Some neat Powershell-snippets for connecting to Office 365 and Exchange Online) on how to achieve that.

So after running: Connect-Online the online-cmdlets Get-MsolGroup and Get-MsolGroupMember becomes available. We will pipe these results to the Exchange Online-cmdlet Get-Recipient (since Get-Mailbox won't work on contacts) to get the RecipientType and RecipientTypeDetails).

At first I couldn't get my head around how Get-MsolGroup -SearchString worked. I tried building my own filters with {Name/UPN/Alias -eq "<upn>"} before reading up on the SearchString-parameter in the official documentation:

Specifies a string. This cmdlet returns security groups that have a display name that start with this string.

This didn't make sense (the parameter should have been -DisplayName) but it worked ;).

The one-liner:

Get-MsolGroup -SearchString "groupName" | % {Get-MsolGroupMember -GroupObjectId $_.ObjectId} | % {Get-Recipient $_.EmailAddress | select DisplayName,RecipientType,RecipientTypeDetails,PrimarySmtpAddress} | clip 
```<br>
Summary/explanation: find a group -> get the group's member(s) from the groups ObjectId -> for each member's EmailAddress get the recipient and select the information wanted from the recipient object for the list -> save it all in clipboard. 

(We could have saved it all to a .csv for instance by `ConvertTo-Csv`-cmdlet instead).

To get all groups just change: `Get-MsolGroup -SearchString "groupName"` to: `Get-MsolGroup -All`

Enjoy!
Andreas Selevik

Andreas Selevik

Solution Architect specialized in Windows & Azure Architecture, Office 365, PowerShell, Identity management and automation. Manchester United fanatic, father, husband and a very good winner...

Read More