Forum Discussion

Freshie's avatar
Freshie
Copper Contributor
Aug 28, 2024

List all DL's in on-prem Exchange with Zero members

We are trying to clean up our 1000's of Distribution Lists before migrating them to 365.

Could somebody please help me with a script that would accomplish this?

I have tried everything I can think of and have struck out miserably.

Thanks in advance.

3 Replies

  • Hey there, ActiveDirectory module is a decent option for this task in on-premises:

    Get-ADGroup -Property Members -Filter * -Server "{DC name}" | where {-not ($_.Members)}
    # Repeat against a DC for each domain.

     Get-ADGroup lets you work with the Members AND MemberOf properties, which are very handy for these kinds of exercises.  But AD module can be difficult if you have multiple domains in the forest...

     

    Enter Get-DistributionGroup, which is fantastic too.. You could try this:

    # in Exchange Management Shell / remote PS Session to Exchange server):
    Set-ADServerSettings -ViewEntireForest $true
    $DGs = Get-DistributionGroup
    $DGsWithoutMembers = foreach ($g in $DGs) {
        if ((Get-DistributionGroupMember $g.Guid.Guid -ResultSize 1 -WarningAction SilentlyContinue | Measure-Object).Count -eq 0) { $DGsWithoutMembers += $g }
    }
    
    # then inspect and/or export:
    $DGsWithoutMembers | Select-Object DisplayName, PrimarySmtpAddress, RecipientTypeDetails, Guid
    $DGsWithoutMembers | Select-Object DisplayName, PrimarySmtpAddress, RecipientTypeDetails, Guid | Export-Csv -Path ".\DGsWithoutMembers.csv" -NTI

     

    • Freshie's avatar
      Freshie
      Copper Contributor
      Great! it looks I was able to use some snippets from your code to get what I needed.

      I also have another question but to my knowledge this is not possible. Is there a way to determine the last time a message was sent to a DL?

      Thanks!
      • JeremyTBradshaw's avatar
        JeremyTBradshaw
        Iron Contributor
        For on-premises, you can use Get-MessageTrackingLog and put the DL into the -Recipient parameter, should work, but only looks back as far as your logs go. In EXO, Message Trace will work too, goes back 90 days max.

        Keeping an eye on some DL's periodically and tracking over time, is the only way I can think of.

Resources