Entra / Microsoft 365 · Exchange Online
Mail traffic statistics
Illustration of how to use the data produced by the Get-MailTrafficSummaryReport cmdlet to create a useful per-mailbox report.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
# Review required modules and connection steps before running.# Connect to Microsoft Graph or Exchange Online as needed for this script.
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
param([int] $LookbackDays = 92,[string] $StartDate = (Get-Date).AddDays(-$LookbackDays); $EndDate = (Get-Date).AddDays(+1),[string] $EndDate = (Get-Date))CLS# Check that we are connected to Exchange Online$ModulesLoaded = Get-Module | Select NameIf (!($ModulesLoaded -match "ExchangeOnlineManagement")) {Write-Host "Please connect to the Exchange Online Management module and then restart the script"; break}[array]$SenderData = Get-MailTrafficSummaryReport -Category TopMailSender -StartDate $StartDate -EndDate $EndDate | Select-Object C1, C2[array]$RecipientData = Get-MailTrafficSummaryReport -Category TopMailRecipient -StartDate $StartDate -EndDate $EndDate | Select-Object C1, C2$MbxReport = [System.Collections.Generic.List[Object]]::new()$Mbx = Get-ExoMailbox -RecipientTypeDetails UserMailbox -ResultSize UnlimitedForEach ($M in $Mbx) {Write-Host "Processing" $M.DisplayName# Check each email proxy address to see if it was used to send email[int]$TotalSentMessages = 0 ; [int]$Messages = 0ForEach ($A in $M.EmailAddresses) {If ($A.Substring(0,4) -eq "smtp") {$Messages = $SenderData | ? {$_.C1 -eq $A.Split(":")[1] } | Select -ExpandProperty C2# Write-Host "Messages found for" $A " " $Messages$TotalSentMessages = ($TotalSentMessages + $Messages) }}# Check each email proxy address to see if it was used to receive email[int]$TotalReceivedMessages = 0 ; [int]$Messages = 0ForEach ($A in $M.EmailAddresses) {If ($A.Substring(0,4) -eq "smtp") {$Messages = $RecipientData | ? {$_.C1 -eq $A.Split(":")[1] } | Select -ExpandProperty C2Write-Host "Messages found for" $A " " $Messages$TotalReceivedMessages = ($TotalReceivedMessages + $Messages) }}$ReportLine = [PSCustomObject] @{User = $M.DisplayNameAddress = $M.UserPrincipalName"Sent messages" = $TotalSentMessages"Received messages" = $TotalReceivedMessages }$MbxReport.Add($ReportLine)}$MbxReport | Out-GridView
Parameters
ParameterDefaultNotes
-LookbackDays92Number of days of mail traffic summary data to include in the report.-StartDate(Get-Date).AddDays(-92); $EndDate = (Get-Date).AddDays(+1)Start of the reporting window.-EndDate(Get-Date)End of the reporting window.Attribution
Author
Office365itpros