Skip to content

Instantly share code, notes, and snippets.

@jschlackman
Last active April 19, 2026 14:03
Show Gist options
  • Select an option

  • Save jschlackman/39f617e93835526499b3a88cc01f5f7e to your computer and use it in GitHub Desktop.

Select an option

Save jschlackman/39f617e93835526499b3a88cc01f5f7e to your computer and use it in GitHub Desktop.
#Requires -Module @{ModuleName='ExchangeOnlineManagement'; ModuleVersion='1.0.1'}
<#
.SYNOPSIS
Gets and optionally exports members of one or more Exchange Online distribution lists or shared mailboxes.
.DESCRIPTION
Gets members of specified Exchange Online distribution lists or shared mailboxes, looks up the recipient/delegate details, and presents them to the user for optional export.
Author: James Schlackman <james@schlackman.org>
Last Modified: Apr 10 2026
.PARAMETER Identity
Specifies the distribution groups, mail-enabled security groups, or shared mailboxes.
.PARAMETER OutputPath
Path to optional CSV export file.
If not specified, a default name will be generated using the current date and display name of the group/mailbox.
.EXAMPLE
Get-SharedMailMembers -Identity marketing@contoso.com -OutputPath 'Marketing Users.csv'
#>
function Get-SharedMailMembers {
Param(
[Parameter(Mandatory)] $Identity,
[Parameter()] [ValidateNotNullOrEmpty()] [String] $OutputPath
)
$rcpts = Get-Recipient -Identity $Identity
$rcpts | Out-Host
foreach ($rcpt in $rcpts) {
Write-Host ("`nRetrieving members for {0} ({1})" -f $rcpt.DisplayName, $rcpt.PrimarySMTPAddress)
If ($rcpt.RecipientType -in ('MailUniversalDistributionGroup','MailNonUniversalGroup','MailUniversalSecurityGroup')) {
# Load distribution group members
$dgMembers = Get-DistributionGroupMember -Identity $Identity
Write-Host ('Group members: {0}' -f @($dgMembers).Count)
$resultList = $dgMembers | Sort-Object -Property PrimarySmtpAddress | Select DisplayName, PrimarySmtpAddress, RecipientTypeDetails, DistinguishedName
} ElseIf ($rcpt.RecipientType -eq 'UserMailbox') {
# Load shared mailbox delegates
$mbPerms = (Get-MailboxPermission -Identity $rcpt).User | Where-Object {$_ -notlike '*\*'} | Sort-Object -Unique
Write-Host ('Shared mailbox delegates: {0}' -f @($mbPerms).Count)
$resultList = foreach($perm in $mbPerms) {
Get-EXORecipient -Identity $perm | Select DisplayName, PrimarySmtpAddress, RecipientTypeDetails, DistinguishedName
}
}
If ($resultList) {
# Set default output path if not specified
If (!($OutputPath)) {
$OutputPath = '{0} {1} Members.csv' -f (Get-Date).ToString('yyMMdd'), $rcpt.DisplayName
}
# Show in grid
Write-Host 'See grid export for details and select records for export.'
$resultExport = $resultList | Out-GridView -OutputMode Multiple -Title 'Select records for export to CSV'
# Export selected records to file
If ($resultExport) {
Write-Host 'Exporting to ' -NoNewline
Write-Host $OutputPath -ForegroundColor Green
$resultExport | Export-Csv -NoTypeInformation -Path $OutputPath -Encoding UTF8bom
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment