Dynamic and Static Distribution Lists

By | July 1, 2021

As if distributions lists weren’t annoying enough (what with the setting them up, the explaining how they work, the explaining what a moderator is and the having to seek and destroy someone’s medical data out of 75,000 user mailboxes when your explanations were ignored) there are two types (static and dynamic) and at rest the dynamic ones have no members – and counting the potential members is a clart on.

So I wrote this script, it find all you static and dynamic DLs, figures out how many people would get an email sent to them and if there’s a moderator then spits the info out as a csv. Super.

#connect-exchangeonline
#DLs

$datestamp = Get-Date -format ddMMyyy_hhmm
$outfile = "./DLs_"+$datestamp+".csv"
$outputs = @()
$i = 0

#Static
$Dls = Get-DistributionGroup -ResultSize unlimited -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
ForEach ($DL in $Dls) {
    $i++
    Write-Host "Processing Static DL" $i "of" $DLs.Count "-" $DL.PrimarySMTPAddress -ForegroundColor Green
    $count = (Get-DistributionGroupMember -Identity $DL.PrimarySMTPAddress -resultsize unlimited -ErrorAction SilentlyContinue -WarningAction SilentlyContinue).PrimarySMTPAddress.Count
    Write-Host "Found" $count "Users" -ForegroundColor Yellow
    $outputs += [PSCustomObject]@{ListType="Static";ListName=$DL.DisplayName;Moderators=$DL.ModeratedBy;Moderated=$DL.ModerationEnabled;PrimarySMTPAddress=$DL.PrimarySmtpAddress;GroupType=$DL.GroupType;Synced=$DL.IsDirSynced;Alias=$DL.Alias;Count=$count}
    
}

#Dynamic
$i=0
$DLs = Get-DynamicDistributionGroup -ResultSize unlimited -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
ForEach ($DL in $DLs) {
    $i++
    Write-Host "Processing Dynamic DL" $i "of" $DLs.Count "-" $DL.PrimarySMTPAddress -ForegroundColor Green
    $count = (Get-Recipient -Filter $DL.RecipientFilter -resultsize unlimited -ErrorAction SilentlyContinue -WarningAction SilentlyContinue).PrimarySMTPAddress.Count
    Write-Host "Found" $count "Users" -ForegroundColor Yellow
    $outputs += [PSCustomObject]@{ListType="Dynamic";ListName=$DL.DisplayName;Moderators=$DL.ModeratedBy;Moderated=$DL.ModerationEnabled;PrimarySMTPAddress=$DL.PrimarySmtpAddress;GroupType=$DL.GroupType;Synced=$DL.IsDirSynced;Alias=$DL.Alias;Count=$count} 
}
 
$outputs | Export-Csv -Path $outfile -NoTypeInformation

It takes ages to run especially doing the filtered get-recipient to get the recipients for dynamic DLs. I gave 10 minutes good solid effort trying to dump all recipients into a variable then parsing the recipientfilter into a select query so I could just reuse the same get-recipient results but it seemed like it was gonna be a lot of work and just letting the thing run for a few hours was easier.

Loading

Leave a Reply

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