To accomplish your task, you'll want to use PowerShell to connect to Exchange Online and retrieve the information for each group. A combination of the Get-Group, Get-Mailbox, and Get-HistoricalSearch cmdlets will be used. However, as of my knowledge cut-off in early 2023, Get-HistoricalSearch does not directly provide the information about the last mail received date. Instead, you may need to use the Get-MessageTrace cmdlet to find the most recent email received by each group within the last 90 days (as Get-MessageTrace typically allows you to search for messages up to the last 90 days).
Below is a sample PowerShell script that connects to Exchange Online, retrieves all groups, checks the last mail received date using Get-MessageTrace, and produces a table with the last mail received date and usage status:
# Connect to Exchange Online
Connect-ExchangeOnline -UserPrincipalName <YourAdminAccount> -ShowProgress $true
# 90 days threshold
$threshold = (Get-Date).AddDays(-90)
# Create an empty array to store the results
$results = @()
# Retrieve all distribution groups
$groups = Get-DistributionGroup
foreach ($group in $groups) {
# Get the most recent message trace for the group within the last 90 days
$lastMessage = Get-MessageTrace -Recipients $group.PrimarySmtpAddress -StartDate (Get-Date).AddDays(-90) -EndDate (Get-Date) | Sort-Object Received -Descending | Select-Object -First 1
# Check if there was a message in the last 90 days
if ($null -eq $lastMessage) {
$lastReceived = $null
$status = "Unused"
} else {
$lastReceived = $lastMessage.Received
if ($lastReceived -lt $threshold) {
$status = "Unused"
} else {
$status = "Used"
}
}
# Add the group information to the results array
$results += New-Object PSObject -Property @{
GroupName = $group.DisplayName
LastMailReceived = $lastReceived
Status = $status
}
}
# Output the results
$results | Format-Table -Property GroupName, LastMailReceived, Status
# Disconnect from Exchange Online
Disconnect-ExchangeOnline -Confirm:$false
Please replace <YourAdminAccount> with your actual Exchange Online admin account.
A few considerations:
-
You need to have the Exchange Online PowerShell Module installed and be an Exchange Online administrator to execute these cmdlets.
-
This script assumes that the distribution groups have an email address associated with them. If they do not, they will not receive emails.
-
The above script only checks the last 90 days because Get-MessageTrace has a limitation on the maximum range of dates it can query.
-
For a more comprehensive search beyond 90 days or if Get-MessageTrace does not provide the necessary data, you might need to use a different approach, such as mailbox audit logging, or examine your organization's data retention and eDiscovery tools to find the last mail received.
-
The script will take some time to execute on environments with a large number of distribution groups.
Keep in mind that if you are unable to find emails for a certain distribution group using Get-MessageTrace, it could be that the group has not received emails in a long time or that the emails are not being traced (e.g., internal emails may not always appear in message traces depending on your organization's configuration).
Always test scripts in a development or test environment before running them in production, and make sure you have the necessary permissions to run the commands in your Exchange Online environment.