Skip to content

Get-DistributionGroupMailbox

Exchange Online: Lists members of a distribution group

#Requires -Version 5.1

[CmdletBinding()]
Param(
    [Parameter(Mandatory = $true)]
    [string]$GroupId,

    [switch]$Nested,

    [ValidateSet('All', 'Users', 'Groups')]
    [string]$MemberObjectTypes = 'All'
)

Process {
    try {
        $result = [System.Collections.ArrayList]::new()

        function Get-NestedMembers {
            param($group)

            $null = $result.Add([PSCustomObject]@{ Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"; MemberType = "Group"; DisplayName = $group.DisplayName; PrimarySmtpAddress = $group.PrimarySmtpAddress })

            if (($MemberObjectTypes -eq 'All') -or ($MemberObjectTypes -eq 'Users')) {
                $users = Get-DistributionGroupMember -Identity $group.Name -ErrorAction Stop | Where-Object { $_.RecipientType -EQ 'MailUser' } | Sort-Object -Property DisplayName
                foreach ($u in $users) {
                    $null = $result.Add([PSCustomObject]@{ Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"; MemberType = "Mailbox"; DisplayName = $u.DisplayName; PrimarySmtpAddress = $u.PrimarySmtpAddress })
                }
            }

            if (($MemberObjectTypes -eq 'All') -or ($MemberObjectTypes -eq 'Groups')) {
                $childGroups = Get-DistributionGroupMember -Identity $group.Name -ErrorAction Stop | Where-Object { $_.RecipientType -EQ 'MailUniversalDistributionGroup' } | Sort-Object -Property DisplayName
                foreach ($cg in $childGroups) {
                    if ($Nested) { Get-NestedMembers $cg }
                    else { $null = $result.Add([PSCustomObject]@{ Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"; MemberType = "Group"; DisplayName = $cg.DisplayName; PrimarySmtpAddress = $cg.PrimarySmtpAddress }) }
                }
            }
        }

        $grp = Get-DistributionGroup -Identity $GroupId -ErrorAction Stop
        if ($null -eq $grp) { throw "Distribution group not found" }

        Get-NestedMembers $grp

        if ($result.Count -gt 0) { Write-Output $result }
        else { Write-Output "No members found" }
    }
    catch { throw }
}

Identity of the distribution group (alias, display name, DN, GUID, or email)

Off

Recursively enumerate nested groups

Filter by member type: All, Users, or Groups

An interactive directory of PowerShell scripts.