Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

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

June 26, 2023 ExchangeMicrosoft 365PowerShell

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.

0 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

How to Connect VPN Before Windows Logon

November 14, 2023

Send Emails with Microsoft Graph API and PowerShell

November 6, 2023

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

October 15, 2023

Leave a Comment Cancel Reply

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

  • How to Connect VPN Before Windows Logon

    November 14, 2023
  • Removing Azure Arc Setup Feature on Windows Server 2022

    November 9, 2023
  • Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

    November 7, 2023
  • Send Emails with Microsoft Graph API and PowerShell

    November 6, 2023
  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Outlook Keeps Asking for Password on Windows
  • How to Manually Configure Exchange or Microsoft 365 Account in Outlook 365/2019/2016
  • FAQ: Licensing Microsoft Exchange Server 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
  • Managing Calendar Permissions on Exchange Server and Microsoft 365
  • Configure Auto-Reply (Out of Office) Message in Exchange and Microsoft 365
Footer Logo

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


Back To Top