Collect count of emails sent to ALL Distribution Groups in ExOL

Anthony-123 340 Reputation points
2025-02-20T00:06:55.62+00:00

Hey Team, I am looking for PowerShell assistance, such as exporting a list of all distribution groups and measuring how many messages were sent to each DG in the past 10 days.

Basically, how can I combine

"Get-DistributionGroup -ResultSize Unlimited | Select-Object Name, DisplayName,GroupType,PrimarySmtpAddress | Export-csv c:\temp\DistributionGroupReport.csv -NoTypeInformation"

and 

"Get-MessageTrace -StartDate (get-date).AddDays(-10) -EndDate (Get-Date) -RecipientAddress email address removed for privacy reasons | measure-object | Select Count"

to a single csv, if possible. Thanks. 

Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,766 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce Jing-MSFT 9,290 Reputation points Microsoft Vendor
    2025-02-20T06:28:15.6166667+00:00

    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:

    User's image


    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.