PowerShell Command for Bulk Message Trace for Multiple Users for 30 Days

Nitishek KR Pandey 0 Reputation points
2026-11-30T18:51:56.0133333+00:00

I am looking for a PowerShell command/script to run a message trace for multiple users at once in Exchange Online.

The requirement is to retrieve both sent and received messages for multiple email addresses for a period of at least 30 days, preferably with the results exported to a single CSV file.

Could you please suggest the recommended PowerShell approach to perform this bulk message trace efficiently?

 

Exchange Online
Exchange Online

A cloud-based service included in Microsoft 365, delivering scalable messaging and collaboration features with simplified management and automatic updates.

0 comments No comments

3 answers

Sort by: Oldest
  1. AI answer

    2026-11-30T18:53:25.3766667+00:00

    Use Get-MessageTraceV2 twice—once with the users as senders and once as recipients—split the 30-day period into intervals of no more than 10 days, continue any 5,000-row result sets, deduplicate them, and export the combined results to one CSV.

    Get-MessageTraceV2 can search the last 90 days, but each query is limited to a 10-day range and 5,000 results. Both SenderAddress and RecipientAddress accept multiple addresses. Larger result sets must be continued using the last row’s Received and RecipientAddress values.^1^

    Bulk 30-day trace script

    Create users.csv:

    EmailAddress
    [Email]
    [Email]
    [Email]
    

    Run the following from Exchange Online PowerShell:

    Connect-ExchangeOnline
    
    $addresses = @(Import-Csv .\users.csv |
        ForEach-Object EmailAddress |
        Where-Object { $_ } |
        Sort-Object -Unique)
    
    $start = (Get-Date).ToUniversalTime().AddDays(-30)
    $end   = (Get-Date).ToUniversalTime()
    $output = "C:\Reports\MessageTrace-30Days.csv"
    New-Item -ItemType Directory -Path (Split-Path $output) -Force | Out-Null
    $results = [System.Collections.Generic.List[object]]::new()
    
    try {
        for ($chunkStart = $start; $chunkStart -lt $end; $chunkStart = $chunkEnd) {
            $chunkEnd = $chunkStart.AddDays(10)
            if ($chunkEnd -gt $end) { $chunkEnd = $end }
    
            foreach ($direction in 'Sent','Received') {
                $queryEnd = $chunkEnd
                $cursorRecipient = $null
                $previousCursor = $null
    
                do {
                    $p = @{
                        StartDate  = $chunkStart
                        EndDate    = $queryEnd
                        ResultSize = 5000
                        ErrorAction = 'Stop'
                    }
                    if ($direction -eq 'Sent') { $p.SenderAddress = $addresses }
                    else { $p.RecipientAddress = $addresses }
                    if ($cursorRecipient) {
                        $p.StartingRecipientAddress = $cursorRecipient
                    }
    
                    $page = @(Get-MessageTraceV2 @p)
                    foreach ($row in $page) {
                        $row | Add-Member DirectionMatched $direction -Force
                        $results.Add($row)
                    }
    
                    if ($page.Count -eq 5000) {
                        $last = $page[-1]
                        $cursor = '{0:o}|{1}' -f $last.Received,
                                                    $last.RecipientAddress
                        if ($cursor -eq $previousCursor) {
                            throw "The continuation cursor did not advance: $cursor"
                        }
                        $previousCursor = $cursor
                        $queryEnd = $last.Received
                        $cursorRecipient = $last.RecipientAddress
                    }
                } while ($page.Count -eq 5000)
            }
        }
    
        $results |
            Sort-Object MessageTraceId, RecipientAddress, Received -Unique |
            Export-Csv -Path $output -NoTypeInformation -Encoding UTF8
    }
    catch {
        Write-Error "Message trace export failed: $($_.Exception.Message)"
        throw
    }
    finally {
        Disconnect-ExchangeOnline -Confirm:$false
    }
    

    The output timestamps are UTC. Adjacent chunks and the separate sent/received searches can overlap—for example, a message sent from one listed user to another—so the final Sort-Object -Unique removes duplicate trace rows. Exchange Online recommends narrow date ranges and precise filters for query performance.^2^

    The documented throttling threshold is 100 requests in a rolling five-minute window. For a large address list or high-volume tenant, divide the input into smaller batches if throttling occurs.^1^

    Permissions

    The account needs message-trace permissions. Supported assignments include:

    • Organization Management in Exchange Online; or
    • Exchange Administrator; or
    • Global Administrator, although Microsoft recommends using the least-privileged role.^2^

    Alternative for very large result sets

    If the expected total is too large for efficient interactive querying, use Start-HistoricalSearch. It produces server-generated CSV reports for data up to 90 days old, with a limit of 100,000 lines per CSV and 250 submitted historical searches per 24 hours.^3^

    Run separate reports for sent and received traffic:

    Start-HistoricalSearch -ReportTitle "Users-Sent-30Days" `
      -ReportType MessageTrace -StartDate $start -EndDate $end `
      -SenderAddress $addresses -NotifyAddress "[Email]"
    
    Start-HistoricalSearch -ReportTitle "Users-Received-30Days" `
      -ReportType MessageTrace -StartDate $start -EndDate $end `
      -RecipientAddress $addresses -NotifyAddress "[Email]"
    

    These commands create two downloadable reports; they do not directly produce one local CSV. Download both from Exchange admin center → Mail flow → Message trace → Downloadable reports, then import, deduplicate, and merge them locally. Reports covering more than 10 days are generated from archived data and can take several hours.^2^


    References

    1. Get-MessageTraceV2-Description
    2. Message trace in the Exchange admin center in Exchange Online
    3. Start-HistoricalSearch-Syntax
    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

  2. Jayden-P 4,565 Reputation points Independent Advisor
    2026-11-30T19:21:01.74+00:00

    Hi @Nitishek KR Pandey

    The approach provided by the AI answer is valid and follows Microsoft's current guidance for bulk message tracing, please give it a check.

    Using Get-MessageTraceV2, splitting the search into 10-day intervals, handling the 5,000-result limit through continuation, and deduplicating the final output are all appropriate for generating a combined 30-day report for multiple users.

    For very large datasets, consider using Start-HistoricalSearch, as Microsoft designed it specifically for large-scale message trace reporting and it can be more efficient than repeated interactive queries.

    Was this answer helpful?

    0 comments No comments

  3. Vasil Michev 128.8K Reputation points MVP Volunteer Moderator
    2026-11-30T19:30:56.4266667+00:00

    For bulk data retrieval scenarios, the recommended approach is using the Graph API based messageTraces method, which I've detailed here: https://michev.info/blog/post/7461/fetch-message-trace-data-via-the-graph-api

    If for some reason you do need to use the PowerShell cmdlets instead, here's a sample script that leverages them to generate per-user statistics: https://michev.info/blog/post/6572/using-the-get-messagetracev2-cmdlet-to-generate-mail-traffic-statistics-by-user

    Both methods allow you to cover up to 90 days of data, however you must run multiple queries, as the response is limited to 10 days at a time.

    Was this answer helpful?

    0 comments No comments

Your answer

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