Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / Exchange / Find Inactive (Unused) Distribution Lists in Exchange/Microsoft 365

March 17, 2024 Azure and Microsoft 365ExchangePowerShell

Find Inactive (Unused) Distribution Lists in Exchange/Microsoft 365

Within your Exchange organization or Exchange Online (Microsoft 365) tenant, you can create hundreds of Distribution Groups (Distribution Lists, DLs). Distribution lists that are no longer in use should be periodically removed by the Exchange administrator. In this article, we’ll show you how to find and delete unused and empty distribution groups in Exchange.

Contents:
  • List Empty Distribution Groups in Exchange with PowerShell
  • How to Find Inactive Distribution Groups in Exchange Server
  • Identify Inactive Distribution Lists in Microsoft 365 (Exchange Online)

Finding unused distribution lists differs from finding inactive AD users or computers. An Exchange DL does not have an attribute like LastLogonDate / LastLogonTimeStamp to track when an object was last used. You can use Exchange tracking logs to determine whether emails have been sent to a particular distribution group.

List Empty Distribution Groups in Exchange with PowerShell

If there are no users in the distribution group, it is likely that it is no longer needed.

You can use PowerShell to find empty distribution groups in an Exchange organization. Connect to your Exchange Server using PowerShell and run the following one-liner:

Get-DistributionGroup –ResultSize Unlimited |Where-Object { (Get-DistributionGroupMember –Identity $_.Name –ResultSize Unlimited).Count -eq 0} | select Name, PrimarySmtpAddress

Find all empty DLs with no members in Exchange

Analyze the resulting list of distribution groups and remove (hide) the DLs you don’t need using the Remove-DistributionGroup command.

Similarly, you can use Get-DynamicDistributionGroup to find empty dynamic DLs:

Get-DynamicDistributionGroup -ResultSize Unlimited | Where-Object { (Get-Recipient -RecipientPreviewFilter (Get-DynamicDistributionGroup -Identity $_.Identity).RecipientFilter).count -eq 0} | select Name, PrimarySmtpAddress

How to Find Inactive Distribution Groups in Exchange Server

The Get-MessageTrackingLog cmdlet is used in Exchange Server to analyze transport logs. For example, you may count the number of e-mail messages sent to a distribution group for the last 90 days using the command below:

Get-MessageTrackingLog -Start (Get-Date).AddDays(-90) -ResultSize unlimited -Recipients "[email protected]"| measure-object

You can use the following command to increase the retention period for the e-mail tracking logs in Exchange Server to 180 days:

Set-TransportService MUN-Ex01 -MessageTrackingLogMaxAge 180.00:00:00

To find unused distribution groups, you can use the following PowerShell script:

  1. Get a list of all distribution groups in a domain and export it to a CSV file:
    Get-DistributionGroup | Select-Object PrimarySMTPAddress | Sort-Object PrimarySMTPAddress | Export-CSV all-exchange-dls.csv –notype
  2. List DLs that received e-mails in the last 30 days:
    Get-MessageTrackingLog -Start (Get-Date).AddDays(-30) -EventId Expand -ResultSize Unlimited |Sort-Object RelatedRecipientAddress | Group-Object RelatedRecipientAddress |Sort-Object Name | Select-Object @{label= "PrimarySmtpAddress";expression={$_.Name}}, Count | Export-CSV exchange-active-dls.csv –notype
    If your Exchange organization has more than one server with a transport role (you can get a list of them using Get-TransportService), you will need to search for each of them: Get-MessageTrackingLog –Server Exh01 ….
  3. Then compare the two lists and find inactive groups:
    $alldl = Import-CSV -Path all-exchange-dls.csv
    $activedl = Import-CSV -Path exchange-active-dls.csv
    Compare-Object $alldl $activedl -Property PrimarySmtpAddress -SyncWindow 500 |Sort-Object PrimarySmtpAddress | Select-Object -Property PrimarySmtpAddress |Export-Csv inactive-dls.csv –NoType
  4. You may hide unused distribution groups in the Global Address List:
    $currentdate = Get-Date
    $notes = "Inactive: hidden in the address list at $currentdate"
    $inactiveDL = Import-CSV -Path inactive-dls.| foreach-object
    {
    Set-Group -identity $_.PrimarySmtpAddress -notes $notes
    Set-DistributionGroup -identity $_.PrimarySmtpAddress -HiddenFromAddressListsEnabled $true
    }

Identify Inactive Distribution Lists in Microsoft 365 (Exchange Online)

In Microsoft 365, you can trace e-mail logs using the Exchange Admin Center (Mail Flow -> Message Trace) or using Start-HistoricalSearch or Get-MessageTrace PowerShell cmdlets. The last command has a significant limitation – it only allows you to search for e-mails that were sent in the previous 10 days. This is not suitable for our task.

Install the Exchange Online PowerShell (EXO) module and connect to your tenant:

Connect-ExchangeOnline

The following command displays the number of e-mails sent to a distribution list’s SMTP address:

Start-HistoricalSearch -ReportTitle "Global Admin DL" -StartDate 04/20/2023 -EndDate 06/20/2023 -ReportType MessageTrace -RecipientAddress global_admins@ woshub.com -NotifyAddress [email protected]

There is a maximum of 250 history searches per 24-hour period that can be used by a single tenant.

To start the search for inactive DLs, you can use the following script::

foreach ($group in Get-DistributionGroup)
{
Start-HistoricalSearch -ReportTitle $group.PrimarySmtpAddress -StartDate 04/20/2023 -EndDate 06/21/2023 -ReportType MessageTrace -RecipientAddress $group.PrimarySmtpAddress -NotifyAddress [email protected]
}

Once the search is complete, you can check how many e-mails have been sent to DL:

Get-HistoricalSearch "Global Admin DL"

If the message list is empty (Rows = 0), it means that the distribution group has not been used in Exchange Online in the last 90 days. Such a distribution list may be considered inactive.

Find inactive distribution lists in Microsoft 365 with PowerShell

You may use Microsoft 365 Groups instead of Distribution Lists in Exchange Online.
1 comment
2
Facebook Twitter Google + Pinterest
previous post
How to Install Remote Server Administration Tools (RSAT) on Windows
next post
Printer Settings Could Not Be Saved, Operation Not Supported

Related Reading

View Windows Update History with PowerShell (CMD)

April 30, 2025

Uninstalling Windows Updates via CMD/PowerShell

April 18, 2025

Allowing Ping (ICMP Echo) Responses in Windows Firewall

April 15, 2025

How to Pause (Delay) Update Installation on Windows...

April 11, 2025

How to Write Logs to the Windows Event...

March 3, 2025

1 comment

Chris October 24, 2024 - 10:34 pm

“Such a distribution list may be considered inactive.” So your saying that any DG that has not been used in 90 days can be deleted? Pretty sure you should say something like “May or may not be inactive, ask it’s members if you can delete it or not” or at least “May or may not be considered inactive”

Reply

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMware
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025
  • How to Write Logs to the Windows Event Viewer from PowerShell/CMD

    March 3, 2025
  • How to Hide (Block) a Specific Windows Update

    February 25, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Outlook Keeps Asking for Password on Windows
  • Checking User Sign-in Logs in Entra ID (Microsoft 365)
  • How to Manually Configure Exchange or Microsoft 365 Account in Outlook 365/2019/2016
  • Search and Delete Emails from User Mailboxes on Exchange Server (Microsoft 365) with PowerShell
  • Fix: Microsoft Outlook Search Not Working on Windows 10/11
  • Blank Sign-in Screen in Office 365 Apps (Outlook, Teams, etc.)
  • Removing Built-in Teams Chat in Windows 11
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top