Note: Before proceed, Connect Exchange Online Remote PowerShell.
The following command gets all the message traffic details for last 7 days.
Get-Messagetrace -Start (Get-Date).AddDays(-7).ToString() -End (Get-Date).ToString()
You need to apply filter if you want to track only non delivered messages.
Get-Messagetrace -Start (Get-Date).AddDays(-7).ToString() -End (Get-Date).ToString() | Where-Object { $_.Status -eq 'Failed'}
The Get-Messagetrace cmdlet returns the columns Received (Message Time), Sender Address, Recipient Address, Subject and Status. For non delivered message, the status column returns the value ‘Failed’ and it doesn’t return the actual reason for the failure message. So, we need to use the another cmdlet Get-MessageTraceDetail to get failure reason of the non delivered message.
The following powershell script lists office 365 non delivered messages and its failure reason.
$failedTraces = Get-Messagetrace -Start (Get-Date).AddDays(-7).ToString() -End (Get-Date).ToString() | Where-Object { $_.Status -eq 'Failed'} $failedTraces | Foreach-Object{ $trace = $_ $stats = $trace |Get-MessageTraceDetail -event FAIL New-Object -TypeName PSObject -Property @{ MessageTime = $trace.Received Sender = $trace.SenderAddress Recipients = $trace.RecipientAddress Subject =$trace.Subject MessageSize = $trace.Size StatusMessage =$stats.Detail }}
Export Office 365 Non Delivery Reports to CSV
We can use the powershell cmedlet Export-CSV to export powershell output into CSV file. The following script exports all non delivered (failed) messages and its failure reason to csv file.
$failedTraces = Get-Messagetrace -Start (Get-Date).AddDays(-7).ToString() -End (Get-Date).ToString() | Where-Object { $_.Status -eq 'Failed'} $failedTraces | Foreach-Object{ $trace = $_ $stats = $trace |Get-MessageTraceDetail -event FAIL New-Object -TypeName PSObject -Property @{ MessageTime = $trace.Received Sender = $trace.SenderAddress Recipients = $trace.RecipientAddress Subject =$trace.Subject MessageSize = $trace.Size StatusMessage =$stats.Detail }} | Export-CSV "C:\Office365NonDeliveryReport.csv" -NoTypeInformation -Encoding UTF8