Exchange2010 : Count sent and received emails per user with Powershell

I wrote this script last month to calculate in Exchange 2010, the number of emails sent and received per user in my company. After that, I send the results by email in HTML format.

Here is the description :

This script will help you to count the total emails sent and received per user in the current month. You can create a schedule task with triggers : “Monthly + last day”The script will run every last day of the month and retrieve informations since the first day of this monthYou

 

I publish my script on the script center of technet gallery : http://gallery.technet.microsoft.com/scriptcenter/Count-sent-and-recceived-f9c66cf7

script

 

Here is the script (you can download here) :

#REQUIRES -Version 2.0 

<# 
.SYNOPSIS  
    Count the total emails sent and received per user in the current month 

.DESCRIPTION 
    Create a schedule task with triggers : "Monthly + last day" 
    The script will run every last day and retrieve informations since the first day of the month 

.NOTES   
    NAME: CountTotalEmailsPerUser.ps1 
    AUTHOR: PRIGENT Nicolas [www.get-cmd.com] 
    v1.2 - 03.01.14 : Send results by email 
    v1.1 - 02.12.14 : Update script with ConvertTo-HTML 
    v1.0 - 01.31.14 : Create the script 

.LINK  
    Script posted over : www.get-cmd.com 

.EXAMPLE  
    Just run ./CountSentRecMailsPerUser.ps1 
#> 

############ Start Import the Exchange 2010 modules if available, otherwise import 2007. 
if (Get-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -Registered -ErrorAction SilentlyContinue) { 
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 
} else { 
    Add-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.Admin 
} 

############ Start Variables 
[Int] $intSent = $intRec = 0 
$emails = Get-Mailbox 
$StartDate = get-date -uformat "%m.01.%Y"  ## Be careful => English date format 
$EndDate = get-date -uformat "%m.%d.%Y"    ## Be careful => English date format 
$tab2 = @() 
$tabInfo = @() 
############ End variables 

############ Start HTML Style 
$head = @' 
<!--mce:0--> 
'@ 
############ End HTML Style 

############ Start retrieve email address + NB sent/received mails 
foreach ($i in $emails) { 

$intRec = 0                       #Number of received mails 
$intSent = 0                      #Number of sent mails 
$address = $i.PrimarySmtpAddress  #Email address 
$address = $address.ToString()    #Email address to string 
$object = new-object Psobject     #Create the object 
$objectInfo = new-object Psobject  #Create the object info 

############ Sent mails 
Get-TransportServer | Get-MessageTrackingLog -ResultSize Unlimited -Start $StartDate -End $EndDate -Sender $address -EventID RECEIVE | ? {$_.Source -eq "STOREDRIVER"} | ForEach { $intSent++ } 

############ Received mails 
Get-TransportServer | Get-MessageTrackingLog -ResultSize Unlimited -Start $StartDate -End $EndDate -Recipients $address -EventID DELIVER | ForEach { $intRec++ } 

############ Insert address + number of sent/received mails 
$object | Add-member -Name "User" -Membertype "Noteproperty" -Value $address 
$object | Add-member -Name "Received" -Membertype "Noteproperty" -Value $intRec 
$object | Add-member -Name "Sent" -Membertype "Noteproperty" -Value $intSent 
$tab2 += $object 
} 

############ Insert informations 
$objectInfo | Add-member -Name "Title" -Membertype "Noteproperty" -Value "Stats Mails" 
$objectInfo | Add-member -Name "Version" -Membertype "Noteproperty" -Value "v1.2" 
$objectInfo | Add-member -Name "Author" -Membertype "Noteproperty" -Value "Nicolas Prigent [www.get-cmd.com]" 
$tabInfo += $objectInfo 

############ Sort by number of sent emails 
$tab2 = $tab2 | Sort-Object Sent -descending 

############ ConvertTo-HTML 
$body =  $tabInfo | ConvertTo-HTML -head $head 
$body += $tab2 | ConvertTo-HTML -head $head 

############ Send emails with results 
send-mailmessage -to "Your_Email@domain.com" -from "StatMails@exchange" -subject "Stats mails From $StartDate To $EndDate" -body ($body | out-string) -BodyAsHTML -SmtpServer "xxx.xxx.xxx.xxx:YY" 

############ end of Script
About Nicolas 282 Articles
I work as an IT Production Manager, based in Paris (France) with a primary focus on Microsoft technologies. I have 10 years experience in administering Windows Servers. . I am a Microsoft MVP for Cloud & Datacenter Management. I also received the PowerShell Hero 2016 award by PowerShell.0rg. And finally, I am "MCSE: Cloud Platform and Infrastructure", "MCSA: Windows Servers", "Administering & Deploying SCCM", and CheckPoint CCSA certified.

Leave a Reply