The follwing script generates e-mail statistics of your exchange environment looking like this:
(Download the script HERE!)
Generated on Server: MAILSERVER
Date: 17.01.2012
Sent mails: 532435
Size of sent mails: 4.419,24 MB
Size of biggest mail out: 21,74 MB
Average size out: 217,69 KB
Quantity incoming mails: 66496
Size of received mails : 4.609,09 MB
Size of biggest mail in: 22,52 MB
Average size in : 161,96 KB
Overall quantity : 43172
Overall size : 9.028,33 MB
You have to change the following parameters:
$SmtpClient.Host = “YOUR_HT_SERVER”
$mailmessage.from = “Exchange_2010_NoReply@YOURDOMAIN.COM”
$mailmessage.To.add(“RECIPIENT@YOURDOMAIN.COM”)
# $mailmessage.CC.add(“CC_RECIPIENT@YOURDOMAIN.COM”)
$mailmessage.Subject = “Exchange daily message Report
$Localhost = $env:COMPUTERNAME
#Load Exchange PS Snapin
If ((Get-PSSnapin | Where-Object {$_.Name -eq “Microsoft.Exchange.Management.PowerShell.E2010”} ).name -eq ‘Microsoft.Exchange.Management.PowerShell.E2010’)
{
Write-Host “Exchange Snapin is already loaded….”
}else
{
Write-Host “Loading Exchange Snapin Please Wait….”; Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
}
Write-Host “running…”$hubs = Get-TransportServer
# Get the start date for the tracking log search
$Start = (Get-Date -Hour 00 -Minute 00 -Second 00).AddDays(-1)# Get the end date for the tracking log search
$End = (Get-Date -Hour 23 -Minute 59 -Second 59).AddDays(-1)$Datum = $Start.ToShortDateString()
$receive = $hubs | get-messagetrackinglog -Start $Start -End $End -EventID “RECEIVE” -ResultSize Unlimited | select Sender,RecipientCount,TotalBytes,Recipients
$send = $hubs | get-messagetrackinglog -Start $Start -End $End -EventID “SEND” -ResultSize Unlimited | select Sender,RecipientCount,TotalBytes
$mreceive = $receive | Measure-Object TotalBytes -maximum -minimum -average -sum
$msend = $send | Measure-Object TotalBytes -maximum -minimum -average -sum$anzahl = $mreceive.count + $msend.count
$volumen = ($mreceive.sum + $msend.sum) / (1024 * 1024)$volumen = “{0:N2}” -f $volumen + ” MB”
$msendmb = $msend.sum / (1024 * 1024)
$vsend = “{0:N2}” -f $msendmb + ” MB”
$bigsend = $msend.maximum / (1024 * 1024)
$avsend = $msend.average / 1024$bigsendmb = “{0:N2}” -f $bigsend + ” MB”
$avsendkb = “{0:N2}” -f $avsend + ” KB”$mreceivemb = $mreceive.sum / (1024 * 1024)
$vreceive = “{0:N2}” -f $mreceivemb + ” MB”
$bigreceive = $mreceive.maximum / (1024 * 1024)
$avreceive = $mreceive.average / 1024$bigreceivemb = “{0:N2}” -f $bigreceive + ” MB”
$avreceivekb = “{0:N2}” -f $avreceive + ” KB”#$senders = $send | Group-Object Sender | Sort-Object Count -Descending
#$topsender = $senders[0].Name
#$topsender += $senders[0].Count#$receivers = $receive | Group-Object Recipients | Sort-Object Count -Descending
#$topreceiver = $receivers[0]
#$topreceiver$computer = gc env:computername
$obj = new-object psObject$obj |Add-Member -MemberType noteproperty -Name “Generated on server:” -Value $Computer
$obj |Add-Member -MemberType noteproperty -Name “Date :” -Value $Datum
$obj |Add-Member -MemberType noteproperty -Name “Sent mails :” -Value $msend.Count
$obj |Add-Member -MemberType noteproperty -Name “Size of sent mails:” -Value $vsend
$obj |Add-Member -MemberType noteproperty -Name “Size of biggest mail out:” -value $bigsendmb
$obj |Add-Member -MemberType noteproperty -Name “Average size out :” -value $avsendkb
$obj |Add-Member -MemberType noteproperty -Name “Quantity incoming mails :” -Value $mreceive.Count
$obj |Add-Member -MemberType noteproperty -Name “Size of received mails :” -Value $vreceive
$obj |Add-Member -MemberType noteproperty -Name “Size of biggest mail in :” -value $bigreceivemb
$obj |Add-Member -MemberType noteproperty -Name “Average size in :” -value $avreceivekb
$obj |Add-Member -MemberType noteproperty -Name “Overall quantity :” -Value $anzahl
$obj |Add-Member -MemberType noteproperty -Name “Overall size :” -Value $volumen$out = $Datum + “;” + $msend.count + “;” + $vsend + “;” + $mreceive.count + “;” + $vreceive + “;” + $anzahl + “;” + $volumen
$out | out-file c:\daily.csv -append -encoding defaultfunction sendmail($body)
{
$SmtpClient = new-object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.Host = “YOUR_HT_SERVER”
$mailmessage.from = “Exchange_2010_NoReply@YOURDOMAIN.COM”
$mailmessage.To.add(“RECIPIENT@YOURDOMAIN.COM”)
# $mailmessage.CC.add(“CC_RECIPIENT@YOURDOMAIN.COM”)
$mailmessage.Subject = “Exchange daily message Report $Datum”
$MailMessage.IsBodyHtml = $false
$mailmessage.Body = $body$smtpclient.Send($mailmessage)
}$obj = $obj -replace(“@{“,””)
$obj = $obj -replace(“=”,”:`t”)
$obj = $obj -replace(“; “,”`n”)
$obj = $obj -replace(“}”,”`n”)sendmail $obj
Cheers,
Chris
Hi Chris,
I have tested your script just right now ! It works great.
Thanks for the good job
Hi Hamid,
thanks alot!
Regards,
Chris
Hi,
Why have you commented out in your script topsenders and topreceivers ?
Hi John,
I commented it out because we’re using mailgateways in front of our Exchange environment an this mailgateways always would be our topsenders/topreceivers so this information isn’t useful in this envrionment.
Regards,
Chris
Could you please add thi code in a raw format, or as an attachment ? As copy/paste breaks the formatting! Example ″ becomes “?” when pasted and etc. Thank you
Hi Aleks, I just put the download link at the top of the article.
Great script to have an overview of the mail traffic.
One question this script also count the system email like mailbox quota messages, delivery and read receipts, etc?
//Chappe
Hi Chris,
I have tested your script and it is a good one. You get a good overview of the email traffic. I have one question. This script counts all email right? This means that system generated messages (mailbox quota messages, delivery and read receipts, etc) are also counted?
Regards,
Leif
Hi Leif, you’re right. The script counts the total amount of mail traffic in the system.
Cheers, Chris
After changing the mentioned parameters, I ran your script on my Exchange 2007 server running Server 2003 x64, but all the totals come back with no totals — “0.00MB” or “0.00KB”.
Also, I’d like to use date range, such as monthly total. I’ve tried to adjust the .AddDays(-1) start date to (-31), but no changes.
Any ideas?
Ah! I found my problem. Your script is for Exchange 2010 and to run with Exchange 2007, I needed to change “Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010” to “Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin”.
I substituted “Admin” for “.E2010” in all references and the script ran great.
Thank you again for the script!! And thank you for sharing with the IT world!
Hi Jeremey,
you’re right.
Thanks for this addition for all Exchange 2007 Users.
Cheers,
Chris
Hiya Chris,
Great work! Thanks a lot
Hi Huseyin,
thanks you very much.
Hello Chris,
It works like a charm but when I enable #$topsender and #$topreceiver it stuck at running state.
Do you have any idea about that issue?
Cheers
Hi,
I tried to figure out, why the script stucks. If I know more, I let you know.
Cheers
Hello Chris,
Thanks for sharing!
A question about the parameters to fill in:
$mailmessage.from = “Exchange_2010_NoReply@YOURDOMAIN.COM”
$mailmessage.To.add(“RECIPIENT@YOURDOMAIN.COM”)
# $mailmessage.CC.add(“CC_RECIPIENT@YOURDOMAIN.COM”)
Do i need to fill in a specific account?
Thanks in advance.
Hello Chris,
Or do i just need to fill in mij domain.com?
Grtz,
Christian
Hi Chrisitan,
you have to fill out sender AND recipient matching to your domain. E.g. $mailmessage.from = “Exchange_2010_NoReply@company.com”
$mailmessage.To.add(“christian@company.com”)
Cheers, Chris
Hi Chris,
We would like to add top senders and top receivers to the reports, is it enough to add following two lines to the script and remove # from the $topsender and $topreceiver?
$obj |Add-Member -MemberType noteproperty -Name “Top senders :” -Value $topsender
$obj |Add-Member -MemberType noteproperty -Name “Top receivers :” -Value $topreceiver
Thanks
Nec
By adding -Recipients or -Sender params the repport can be spesified by account. Thanks for sharing!
Hi Chris,
Important correction: EventID RECEIVE is not for received emails, it is for sent emails. You should replace RECEIVE as DELIVER to get the correct number of received emails.
BTW, Senders include internal and external senders, so it is total of inbound and outbound traffic, not only outbound.
Your scripts counts senders (inbound and outbound) and recipients (internal), you might need to change your titles accordingly.
hi chris,
We have exchange 2007 on windows 2003. Will this script work .
Regards,
Sam
How to find the sent item counts for shared mailbox?
hi, quick question, what date range is this calculating on? is only one day? as per # Get the start date for the tracking log search
$Start = (Get-Date -Hour 00 -Minute 00 -Second 00).AddDays(-1)
# Get the end date for the tracking log search
$End = (Get-Date -Hour 23 -Minute 59 -Second 59).AddDays(-1
is there no way to calculate for an entire month?
[…] Viewing total incoming/outgoing email statistics (Script) […]
Hi Chris, thank you for being so kind as to put this out there!
I have a shared mail box in outlook 2010 and I want to calculate how many emails have come into the inbox over a 7 day / week period and mail me the result – is this possible? Ive tried all combos of all the information above but not luck, just lots of errors…
Suzy
Hello Chris,
Thank-you for the useful script.
What can be done to modify the script to pull date from an entire month or manually specifying a specific day ?
Thanks,
Keith
hi Keith
I had the same question and playing around with numbers I found if you change the last number you get as many days you want, in this example I got a month 30 days
# Get the start date for the tracking log search
$Start = (Get-Date -Hour 00 -Minute 00 -Second 00).AddDays(-30)
I hope this help
# Get the start date for the tracking log search
$Start = (Get-Date -Hour 00 -Minute 00 -Second 00).AddDays(-30)
change the number at the end and you can get a month
Why does the subject need modifying? I get an error after running the script for the subject.
$mailmessage.Subject = “Exchange daily message Report
Error: The term ‘â?oExchange’ is not recognized as the name of a cmdlet, function, script file, or operable program.
Hi,
I tried to run it but its showing error as below:please suggest:
Exception calling “Send” with “1” argument(s): “The SMTP server requires a secu
re connection or the client was not authenticated. The server response was: 5.7
.1 Client was not authenticated”
At E:\MailFlow\TotalEmailsSentReceivedPerDay.ps1:89 char:21
+ $smtpclient.Send <<<< ($mailmessage)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
For those looking to increase the range of the search beyond one day… Keep in mind the $Start is the past day and $end is today. He uses a strange to me way of getting those but Im no PS expert. If you change $Start = (Get-Date -Hour 00 -Minute 00 -Second 00).AddDays(-30) It will pull 30 days as long as your logs go back that far.
hi how do i set a date range? i.e. i need stats for a month’s worth of emails…any help would be most appreciated.