Powershell to get Complete Mailbox Statistics in the Exchange 2007 Orginisation
Posted by Krishna - MVP on May 14, 2009
Below is the powershell command to export the complete details of the Mailbox in the Exchange Organization. Details like Name, Mailbox size (MB), Mailcount, Mailbox limits, Account Active or disabled, CreatedDate, Lastlogon time, Last logofftime etc into the CSV file. This Data can even imported in to the SQL database and Query to analyst the growth of the mailbox. If you collect these details on Daily basis and uploaded into SQL then you can easily get the details of the mailbox in seconds for auditing or capacity management etc.
With SQL integration you can query details like Top 100 mailbox, Top 100 Fastest growing mailbox, List of all active maibox, List of disabled mailbox, List of mailbox out of default quota limits, maiboxes not used in past few days. Newly created mailbox in past one month etc
Get-Mailbox -ResultSize Unlimited | select DisplayName, Alias, Database, PrimarySmtpAddress,@{name=’IssuewarningQuota’;expression={if ($_.IssueWarningQuota -match “UNLIMITED”) {“-1″} else {$_.IssueWarningQuota.value.tomb() }}},@{name=’ProhibitSendQuota’;expression={if ($_.ProhibitSendQuota -match “UNLIMITED”) {“-1″} else {$_.ProhibitSendQuota.value.tomb() }}},@{name=’ProhibitSendReceiveQuota’;expression={if ($_.ProhibitSendReceiveQuota -match “UNLIMITED”) {“-1″} else {$_.ProhibitSendReceiveQuota.value.tomb() }}},WhenCreated |export-csv C:\stats.csv
“DisplayName,Alias,MailboxSizeMB,ItemCount,PrimarysmtpAddress,IssueWarningQuotaMB,ProhibitSendQuotaMB,ProhibitSendReceiveQuotaMB,DatabaseName,CreationDate,LastLogonTime,LastLogoffTime,isActive” | out-file C:\MailstatsResult.csv
$csv = Import-csv -path “C:\stats.csv”
foreach($line in $csv)
{
$MailboxStats = Get-MailboxStatistics $Line.Alias | Select TotalItemSize,Itemcount,LastLogoffTime,LastLogonTime
$L = “{0:N0}” -f $mailboxstats.totalitemsize.value.toMB()
$Size = “”
$Len = $L.Split(‘,’)
for ($i=0; $i -lt $Len.length; $i++)
{
$Size = $Size +$Len[$i]
}
$temp=$Line.PrimarysmtpAddress
$adobjroot = [adsi]”
$objdisabsearcher = New-Object System.DirectoryServices.DirectorySearcher($adobjroot)
$objdisabsearcher.filter = “(&(objectCategory=Person)(objectClass=user)(mail= $Temp)(userAccountControl:1.2.840.113556.1.4.803:=2))”
$resultdisabaccn = $objdisabsearcher.findone() | select path
if($resultdisabaccn.path)
{
$actStatus = “1″
}
Else
{
$actStatus = “0″
}
$out =$Line.Displayname + “,” + $Line.Alias + “,” + $Size + “,” + $MailboxStats.ItemCount + “,” + $Line.PrimarySmtpAddress + “,” + $Line.IssueWarningQuota + “,” + $Line.ProhibitSendQuota + “,” + $Line.ProhibitSendReceiveQuota + “,” + $Line.Database + “,” + $Line.WhenCreated + “,” + $MailboxStats.LastLogonTime + “,” + $MailboxStats.LastLogoffTime + “,” + $actStatus
$Out | Out-File C:\MailstatsResult.csv -Append
}
You can get the copy of the script in the below text file


Andreas Bergman said
I might be stupid, but i can’t get the script to run om my machine. Do I need to run on the exchange server?
Krishna said
Hi,
You can Execuite both in Exchagne server and local machine Exchange powershel console
You can get faster result if you execute on Exchange server.
Regards,
Krishna
Greg B said
Great script. Very useful. However, if one has multiple domains, you need to add -ignoredefaultscope right after the get-mailbox command. This will return all mailboxes from the forest, not just from the current scope.
Krishna said
Hi,
Thank you for your valuable comments
Regards,
Krishna
Mario said
Hi,
Excellent script, but how can we include AD attribute “title”, tried to modify the script to add it but cannot seem to display the attribute in the report generated.
You advice is appreciated.
Thank you.
Rosio Shere said
Hello just thought i would let you know something.. This can be twice now i?ve landed on your weblog inside the last 3 weeks trying to find totally unrelated issues. Great Data! Keep up the great get the job done.
Wubbo Oxhol said
Very nice indeed.
Does anyone know how to alter this to include first name, last name and all emailaddresses of the mailboxes?
Narayana said
Excellent script. Thank you.
Can you please help me understand, what does the Active status value specify? is that based on usage?
thanks
Narayana
Kenny Dillon said
Amazing! It is hard to get Exchange to spill its guts. Thank you!!
-Kenny Dillon-
Administrator Monkey
Corey Zamara said
is it just me or does the CSV not come out correct, in that it’s not proper delimited and not seperated by a coloum in excel?
Sundar said
Hi
We are using exchange 2010 SP1. I need to prepare every month full report. I found your script very helpful. I just want to know whether we can include OU information as well. Because I need add which user belong to which OU. So I am manually exporting recipients list from EMS and past into excel sheet then I am comparing both excel sheet and merging. This is hectic job. Is there a script to get a report including OU? If so please let me know.
Regards,
Sundar
ReMark said
Hi,
How do you import the data into SQL?