|
#Requires -Module @{ModuleName='ExchangeOnlineManagement'; ModuleVersion='1.0.1'} |
|
|
|
<# |
|
.SYNOPSIS |
|
Gets and optionally exports members of an Exchange Online distribution list or shared mailbox. |
|
|
|
.DESCRIPTION |
|
|
|
Gets members of an Exchange Online distribution list or shared mailbox, looks up the recipient/delegate details, and presents them to the user for optional export. |
|
|
|
Author: James Schlackman <james@schlackman.org> |
|
Last Modified: Mar 13 2026 |
|
|
|
.PARAMETER Identity |
|
Specifies the distribution group, mail-enabled security group, or shared mailbox. |
|
|
|
.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 |
|
) |
|
|
|
$rcpt = Get-Recipient -Identity $Identity |
|
$rcpt | Out-Host |
|
|
|
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 |
|
} |
|
} |
|
} |