Skip to content

Instantly share code, notes, and snippets.

@junecastillote
Created August 17, 2023 04:03
Show Gist options
  • Save junecastillote/b5727db2c6ae1600593ff862347cd1ef to your computer and use it in GitHub Desktop.
Save junecastillote/b5727db2c6ae1600593ff862347cd1ef to your computer and use it in GitHub Desktop.
Export Distribution List Members PowerShell Script
# Export-DLMembers.ps1
[CmdletBinding()]
param (
[Parameter()]
$DistributionList
)
if ($DistributionList) {
try {
$group = Get-DistributionGroup -Identity $DistributionList -ErrorAction Stop
}
catch {
$_.Exception.Message | Out-Default
return $null
}
}
else {
try {
$group = Get-DistributionGroup -ResultSize Unlimited -ErrorAction Stop | Sort-Object DisplayName
}
catch {
$_.Exception.Message | Out-Default
return $null
}
}
if ($group.Count -gt 0) {
for ($i = 0; $i -lt $group.Count; $i++) {
Write-Progress -Activity "Distribution List: $($group[$i].DisplayName)" -Status "$($i+1) of $($group.Count) " -PercentComplete (($($i + 1) / $($group.Count)) * 100)
try {
Get-DistributionGroupMember -ResultSize Unlimited -Identity $group[$i] -ErrorAction Stop |
Select-Object @{n = "GroupGUID"; e = { $group[$i].GUID } },
@{n = "GroupName"; e = { $group[$i].Name } },
@{n = "GroupEmail"; e = { $group[$i].PrimarySmtpAddress } },
@{n = "MemberGUID"; e = { $_.GUID } },
@{n = "MemberName"; e = { $_.DisplayName } },
@{n = "MemberEmail"; e = { $_.PrimarySmtpAddress } },
@{n = "MemberType"; e = { $_.RecipientTypeDetails } }
}
catch {
$_.Exception.Message | Out-Default
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment