How to get all Groups with Members in Exchange Online PowerShell

This post is Part 2/3 of Office 365 groups VS Exchange Online groups.


Exchange Online Groups

You need the Exchange Online module for PowerShell.
You can find it here.

MFA enabled accounts can install the module from Exchange Admin Center > Hybrid > Install MFA Module. You’ll need to use Internet Explorer or Microsoft Edge.

Exchange Online Multi Factor authentication Module for PowerShell.
Exchange Online Multi Factor authentication Module for PowerShell.

After you have installed the module you can log in to Exchange Online with the following command.

Without Multi Factor Authentication (MFA):

$cred365 = get-credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred365 -Authentication Basic -AllowRedirection
Import-PSSession $Session

With Multi Factor Authentication (MFA):

param(
    [parameter(Mandatory = $True)][string]$username
)

Import-Module $((Get-ChildItem -Path $($env:LOCALAPPDATA + "\Apps\2.0\") -Filter Microsoft.Exchange.Management.ExoPowershellModule.dll -Recurse ).FullName|? {$_ -notmatch "_none_"}|select -First 1)

$EXOSession = New-ExoPSSession -UserPrincipalName $username
Import-PSSession $EXOSession

Now that we are logged in, we can start getting a Distribution Group. Suppose we have a group in Exchange online called Group5, then we can use the following command.
For more about Get-DistributionGroup click here.

Get-DistributionGroup -Identity Group5

To get the members of Group5 we can use the following command.
For more about Get-DistributionGroupMember click here.

Get-DistributionGroupMember -Identity Group5

To get all Distribution Groups in Exchange Online we use -ResultSize Unlimited.

Get-DistributionGroup -ResultSize Unlimited

So now we do this for one Distribution Group. You can also do this for all Distribution Groups in Exchange Online. Take a look at Paragraph ExO Full Script.

Here I retrieve all Exchange Online Distribution Groups in an array and then I read the members for each group. I then add it to a new PSObject and add it to $Data per member.

Now you could export in $Data to a CSV or text file.

You can use this for CSV. For more about Export-CSV click here.

$Data| Export-CSV -Path C:\temp\EXOgroups.csv -NoTypeInformaton

And this for text files. For more about Out-File click here.

$Data | Out-File -FilePath C:\temp\EXOGroups.txt

Suppose you only want the members of a certain set of groups you can use -Filter with Get-DistributionGroup.

As an example I get all the groups that are Security Enabled in Exchange online.

Get-DistributionGroup -ResultSize Unlimited -Filter {GroupType -Like "*SecurityEnabled*"}

ExO Full Script

$DGS = Get-DistributionGroup -ResultSize Unlimited

$Data = @()

Foreach ($DG in $DGS)

{

    Foreach ($Member in (Get-DistributionGroupMember -Identity $DG.PrimarySmtpAddress))

    {

        $DataObject = New-Object PSObject  

        $DataObject | Add-Member -NotePropertyName "Member" -NotePropertyValue $Member.PrimarySmtpAddress

        $DataObject | Add-Member -NotePropertyName "Distribution Group" -NotePropertyValue $DG.PrimarySmtpAddress

        $Data += $DataObject

    }

}

Any questions?

Do you want to know more or do you have any questions about the Exchange Online part? Then leave a comment.


A little extra

This post contains PowerShell. Would you like to learn the basics better? I have created a new website to learn basic PowerShell in an ’emulator’ environment.
Click here to go learn Basic PowerShell.

Published by

Bas Wijdenes

My name is Bas Wijdenes and I work full-time as a Services Engineer. In my spare time I write about the error messages that I encounter during my work. Furthermore, I am currently occupied with Office 365, Azure infrastructure, and PowerShell for automating daily tasks.

Leave a Reply

Your email address will not be published. Required fields are marked *