Hi,@Anthony-123
Thanks for posting your question in the Microsoft Q&A forum.
Combining the output of distribution groups with the message trace count in a single CSV file can be done by iterating through each distribution group, performing the message trace for each one, and then collecting the results into a single object that you export to CSV at the end.
# Define the end date and start date for the message trace
$EndDate = Get-Date
$StartDate = $EndDate.AddDays(-10)
# Create an array to store the results
$results = @()
# Get all distribution groups
$distributionGroups = Get-DistributionGroup -ResultSize Unlimited
# Loop through each distribution group
foreach ($group in $distributionGroups) {
# Get the count of messages sent to the distribution group in the last 10 days
$messageCount = (Get-MessageTrace -StartDate $StartDate -EndDate $EndDate -RecipientAddress $group.PrimarySmtpAddress | Measure-Object).Count
# Create a custom object to hold the distribution group information and the message count
$result = [PSCustomObject]@{
Name = $group.Name
DisplayName = $group.DisplayName
GroupType = $group.GroupType
PrimarySmtpAddress = $group.PrimarySmtpAddress
MessageCount = $messageCount
}
# Add the custom object to the results array
$results += $result
}
# Export the results to CSV $results | Export-Csv c:\DistributionGroupReportWithMessageCounts.csv -NoTypeInformation
The test results are as follows:
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.