- Custom Message Trace Retention (Roadmap ID 542929): Rolling out in December 2026, Exchange Online administrators can choose predefined retention periods for Message Trace data to align with regional privacy mandates (e.g., GDPR data minimization) or internal audit requirements—replacing the rigid one-size-fits-all retention model.
Get-MessageTraceV2Rules: Supports up to 90 days of historical trace data, but enforces a strict 10-day maximum span per query and a 5,000-result cap per call.- Cursor Pagination Replaces
-Page: Unlike legacyGet-MessageTrace, V2 removes the-Pageparameter. To export large datasets without missing events, scripts must paginate using-EndDateand-StartingRecipientAddress.
For years, Exchange Online administrators had no control over how long Message Trace metadata lived in the Exchange Admin Center (EAC)—and security teams needing 6-month or 1-year mail-flow audit trails had to build brittle polling jobs against the legacy Get-MessageTrace cmdlet.
In December 2026, Microsoft is rolling out Custom Retention for Message Trace Logs (Microsoft 365 Roadmap ID 542929), giving admins predefined retention choices to match corporate compliance and privacy policies. At the same time, Message Trace V2 (Get-MessageTraceV2) is now the standard PowerShell interface for querying up to 90 days of mail flow telemetry. This guide explains how to plan your retention window and provides a live-tested PowerShell archiving script that handles 10-day window chunking and V2 cursor pagination cleanly.
Architecture: Automated Get-MessageTraceV2 Chunking & Cursor Pagination
1. Key Differences: Legacy Get-MessageTrace vs. Get-MessageTraceV2
| Capability | Legacy Get-MessageTrace (Deprecated) | Modern Get-MessageTraceV2 (2026 Standard) |
|---|---|---|
| Max Historical Depth | 10 days (older required async Start-HistoricalSearch) | 90 days direct queryable history |
| Max Single Query Span | 10 days | 10 days (throws error if span exceeds 10 days) |
| Default Timeframe (No Dates) | Last 48 hours | Last 48 hours |
| Pagination Mechanism | -Page and -PageSize | Cursor-based: -EndDate + -StartingRecipientAddress |
| Max Results Per Call | 5,000 | 5,000 (returns warning with exact next cursor parameters) |
| Auth Support | EXO PowerShell | EXO PowerShell v3.7+ (App-Only Cert Auth recommended) |
Live-Verified Pagination Behavior (LAB 365 Dev Tenant)
WhenGet-MessageTraceV2 hits -ResultSize, Exchange Online returns a warning showing the exact cursor needed for the next batch:
WARNING: There are more results, use the following command to get more.
Get-MessageTraceV2 -StartDate "2026-12-01T07:25:20.0000000Z" -EndDate "2026-12-01T10:38:09.1800000Z" -StartingRecipientAddress "[email protected]" -ResultSize 5
Notice that results are returned in reverse chronological order (newest first). To fetch the next page, you pass the Received timestamp and RecipientAddress of the last item in the batch into -EndDate and -StartingRecipientAddress.
2. Production PowerShell Script: Automated Get-MessageTraceV2 Archiver
This script connects unattended using Certificate-Based App-Only Authentication (Connect-ExchangeOnline -AppId ... -CertificateThumbprint ...), slices any requested window (e.g., 30 or 90 days) into compliant 10-day chunks, follows V2 cursor pagination automatically, deduplicates boundary rows, and writes clean CSV archives.
#Requires -Modules ExchangeOnlineManagement
<#
.SYNOPSIS
Automated Exchange Online Message Trace V2 archiver with 10-day chunking and cursor pagination.
.DESCRIPTION
Live-tested against Exchange Online (ExchangeOnlineManagement v3.11+).
Handles Get-MessageTraceV2's 10-day query span cap and 5,000-row cursor pagination
using -EndDate and -StartingRecipientAddress.
.EXAMPLE
.\Export-MessageTraceV2Archive.ps1 -Organization "contoso.onmicrosoft.com" -AppId "<GUID>" -CertificateThumbprint "<THUMB>" -DaysBack 30
#>
param(
[Parameter(Mandatory)]
[string]$Organization,
[string]$AppId,
[string]$CertificateThumbprint,
[ValidateRange(1, 90)]
[int]$DaysBack = 14,
[string]$SenderAddress,
[string]$RecipientAddress,
[int]$BatchSize = 5000,
[string]$OutputFolder = ".\MessageTraceV2-Archive"
)
$ErrorActionPreference = "Stop"
if (-not (Test-Path $OutputFolder)) { New-Item -ItemType Directory -Path $OutputFolder | Out-Null }
# 1. Connect to Exchange Online (Unattended Cert Auth preferred)
if ($AppId -and $CertificateThumbprint) {
Connect-ExchangeOnline -AppId $AppId -CertificateThumbprint $CertificateThumbprint -Organization $Organization -ShowBanner:$false
} else {
Connect-ExchangeOnline -Organization $Organization -ShowBanner:$false
}
$UtcNow = (Get-Date).ToUniversalTime()
$GlobalStart = $UtcNow.AddDays(-$DaysBack)
$ChunkEnd = $UtcNow
$SeenKeys = [System.Collections.Generic.HashSet[string]]::new()
$AllRecords = [System.Collections.Generic.List[object]]::new()
Write-Host "[+] Exporting Message Trace V2 from $($GlobalStart.ToString('u')) to $($UtcNow.ToString('u'))..." -ForegroundColor Cyan
# 2. Slice total range into <= 10-day windows
while ($ChunkEnd -gt $GlobalStart) {
$ChunkStart = $ChunkEnd.AddDays(-10)
if ($ChunkStart -lt $GlobalStart) { $ChunkStart = $GlobalStart }
Write-Host " -> Querying window: $($ChunkStart.ToString('yyyy-MM-dd HH:mm')) UTC to $($ChunkEnd.ToString('yyyy-MM-dd HH:mm')) UTC" -ForegroundColor Yellow
$CursorEnd = $ChunkEnd
$CursorRecipient = $null
$PageNumber = 0
do {
$PageNumber++
$Params = @{
StartDate = $ChunkStart
EndDate = $CursorEnd
ResultSize = $BatchSize
}
if ($CursorRecipient) { $Params['StartingRecipientAddress'] = $CursorRecipient }
if ($SenderAddress) { $Params['SenderAddress'] = $SenderAddress }
if ($RecipientAddress) { $Params['RecipientAddress'] = $RecipientAddress }
$Batch = @(Get-MessageTraceV2 @Params -WarningAction SilentlyContinue)
$Added = 0
foreach ($Row in $Batch) {
$UniqueKey = "$($Row.MessageTraceId)|$($Row.RecipientAddress)"
if ($SeenKeys.Add($UniqueKey)) {
$AllRecords.Add([PSCustomObject]@{
ReceivedUTC = $Row.Received.ToUniversalTime().ToString("o")
SenderAddress = $Row.SenderAddress
RecipientAddress = $Row.RecipientAddress
Subject = $Row.Subject
Status = $Row.Status
FromIP = $Row.FromIP
ToIP = $Row.ToIP
SizeBytes = $Row.Size
MessageId = $Row.MessageId
MessageTraceId = $Row.MessageTraceId
})
$Added++
}
}
Write-Host " Page $PageNumber : Retrieved $($Batch.Count) rows ($Added unique)" -ForegroundColor Gray
# Advance cursor if batch hit the limit
if ($Batch.Count -eq $BatchSize) {
$LastRow = $Batch[-1]
$CursorEnd = $LastRow.Received.ToUniversalTime()
$CursorRecipient = $LastRow.RecipientAddress
} else {
break
}
} while ($true)
$ChunkEnd = $ChunkStart
}
$Timestamp = (Get-Date).ToString("yyyyMMdd-HHmmss")
$ExportPath = Join-Path $OutputFolder "MessageTraceV2-$Timestamp.csv"
$AllRecords | Export-Csv -Path $ExportPath -NoTypeInformation -Encoding UTF8
Disconnect-ExchangeOnline -Confirm:$false -ErrorAction SilentlyContinue
Write-Host "`n[+] Archived $($AllRecords.Count) unique message trace events to $ExportPath" -ForegroundColor Green
3. Governance Recommendations for Custom Message Trace Retention (Roadmap 542929)
- Align EAC Retention with Privacy Policy: If your legal or DPO team mandates 30-day operational log minimization in M365, set the Exchange Online Message Trace retention accordingly once Roadmap 542929 reaches your tenant—while archiving security-relevant headers (without subject lines if required by PII rules) to Microsoft Sentinel or immutable Azure Blob Storage.
- Schedule Daily Incremental Exports: Instead of pulling 90 days at once, schedule
Export-MessageTraceV2Archive.ps1 -DaysBack 2daily via Azure Automation or a hardened scheduled task using App-Only Certificate Auth. The script'sMessageTraceId|RecipientAddressdeduplication ensures zero overlap. - Pair with
Get-MessageTraceDetailV2for Incident Triage: When investigating a specific delivery failure or transport rule hit, pipe theMessageTraceIdandRecipientAddressintoGet-MessageTraceDetailV2for full hop-by-hop diagnostic events.
Related LAB 365 Guides
- M365 Admin Handbook Ch. 5: Enterprise Mail Flow, Connectors, Transport Rules & Message Trace V2
- Connect-ExchangeOnline
-CredentialDeprecation: Unattended Certificate Auth Guide - Exchange Online Transport Enforcement System Throttling Runbook (450/550 5.7.230)
đź’¬ Reader Comments & Feedback
Did this runbook help, or did Microsoft change a cmdlet or UI menu? Share your feedback below.
Leave a Comment or Report an Update