The following script will return a list of the email groups a Office 365 user is a member of. Useful if you are creating a new user and want to to be in the same groups.
Connecting to exchange
You can either run the command from a local PowerShell session after installing e EXO V2 module. Or I find it easier to use the Cloud Shell from the Exchange Online Admin Centre, up to you.
Method 1 – Cloud Shell
- Browse to and logon to your Exchange Admin centre https://admin.exchange.microsoft.com/
- Click the Cloud Shell icon in the top right. If this is your first time using Cloud Shell you will get a quick setup wizard, just follow the steps
- Enter the command Connect-EXOPSSession to connect to Exchange and wait for the connection to complete
Method 2 – Local PowerShell session
- If you haven’t already you will need to install the Exchange Online PowerShell V2 module by running the command Install-Module -Name ExchangeOnlineManagement
- Run the command Connect-ExchangeOnline -UserPrincipalName <UPN> to logon. Replacing <UPN> with your Office 365 admin username.
Getting a users email group membership
Change [email protected] to the email address for the user to query.
$Mailbox=get-Mailbox [email protected]
$Filter = "Members -like ""$DN"""
Get-DistributionGroup -ResultSize Unlimited -Filter $Filter