PowerShell Script To Determine Number Of Emails Sent To Per External Domain

Exchange PFEs Daya Patil and Abdul Rafey Mohammed share one of their recent PowerShell scripts to analyse Exchange message tracking logs. 

One of our customers needed a quick way to determine the number of emails being sent from their Exchange 2010 organization to external domains, and with a count of per each external domain.

We made a script to get this information, and wanted to share for those with similar requirements. 


In the script, we determine the number of emails sent from each Mailbox Server between  two defined time periods.  Script parameters can be changed according to your requirements. 



Script Code



#This Sample Code is provided for the purpose of illustration only and is not intended to be used in a #production environment. THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" #WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO #THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We #grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and #distribute the object code form of the Sample Code, provided that You agree: (i) to not use Our name, #logo, or trademarks to market Your software product in which the Sample Code is embedded; (ii) to #include a valid copyright notice on Your software product in which the Sample Code is embedded; and #(iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or #lawsuits, including attorneys’ fees, that arise or result from the use or distribution of the Sample Code."

#"This sample script is not supported under any Microsoft standard support program or service. The #sample script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied #warranties including, without limitation, any implied warranties of merchantability or of fitness for a #particular purpose. The entire risk arising out of the use or performance of the sample scripts and #documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in #the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, #without limitation, damages for loss of business profits, business interruption, loss of business #information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or #documentation, even if Microsoft has been advised of the possibility of such damages"




#Start Customization Here#

$MbServer = "<Server Name>"

$StartFrom = "<MM/DD/YYYY HH:MM:SS>"

$EndAt = "<MM/DD/YYYY HH:MM:SS>"

$PathForOPFile = "<Output Folder>"

#End Customization Here#



[String[]]$ExternalDomains = @()

$Site=(Get-ExchangeServer $MbServer).Site


$Data = Get-ExchangeServer | Where-Object {$_.IsHubTransportServer -eq $true -and $_.Site -Like "*$SiteToParse*"} | Get-MessageTrackingLog -ResultSize Unlimited -EventId Receive -Start $StartFrom -end $EndAt| Where-object {($_.Recipients -ne $null) -and ($_.Source -eq "STOREDRIVER") -and ($_.ClientHostName -Like "*$MbServer*")} |Select-Object Recipients

[String[]]$Rcpt1 = @()

Foreach ($Entry in $Data)


$Rcpt1 += ($Entry).Trim("{ }").Split(“,”)


$RecipientDomList =$Rcpt1

Foreach ($Domain in $RecipientDomList)


if ($Domain -NotLike "*@InternalDomain.Com")


$ExternalDomains += $Domain.Split("@")[1]



$ExternalDomains | Group-Object | Select Name, Count |Export-CSV "$PathForOPFile \op.csv" –NoType


Published by MSPFE Editor Rhoderick Milne.