How to get mailbox statistics in Exchange 2007
If you have been managing Exchange for any length of time, you know you have to do some things at the command line. While you might prefer the GUI, once you start to work with the Exchange Management Shell you’ll really start to appreciate just how powerful it is, and that there are some things that are almost impossible to do in the GUI which are trivially easy in the shell. When it comes to managing Exchange data, a very useful command for gathering information on mailboxes and mailbox databases is the Get-MailboxStatistics command in the Exchange Management Shell.
Get-MailboxStatistics is a very useful and feature rich way to get mailbox statistics from Exchange 2007 servers. You can use this command to generate an Exchange 2007 mailbox size report on an individual mailbox, or all the mailboxes within a specific database or server. You can also get information on the mailbox size, number of objects, reports, database information, and history and display this to screen, or you can dump the information to a CSV file.
How to use it
Try the following commands on your own server as we go through them. To use the command, open an Exchange Management Shell on your mailbox server, and enter the command by itself as:
Get-MailboxStatistics [enter]
On an Exchange 2007 mailbox server, this will return a formatted list of all mailboxes, the number of items in each mailbox, whether or not they have exceeded their quota, and the last time they logged on. As with every cmdlet, you can use the Get–Help and –examples modifiers to get more information about this command, but we’ll go over some useful tricks in the following sections.
Formatting
The default output of the Get-MailboxStatistics command is useful if you want tabular data, but by piping this through the Format-List command (aliased as FL), you can get even more information.
Get-MailboxStatistics | fl [enter]
Note that you get more information here, including the number of deleted items, the last logged on user, which database stores the mailbox, its GUID, and more. Because we chose to format this as a list, our data is displayed on attribute per line, and each user is listed in his/her own section of the output. If you want to see only one user, you can use the –Identity modifier, like this:
Get-MailboxStatistics –Identity domain\username | fl [enter]
When you specify an identity, you can use the sAMAccountName, UPN, alias, primary SMTP address, or any other SMTP alias.
You can also pipe the output through the Format-Table command (aliased FT) to create a table listing only the desired attributes. Try this command where each attribute you want to see is listed, separated by commas.
Get-MailboxStatistics –Identity alias | ft DisplayName, ItemCount, TotalItemSize, DeletedItemCount [enter]
You can then export this (or any other output table) to a CSV file for further analysis or reporting by then piping through the Export-CSV command, like this:
Get-MailboxStatistics | ft DisplayName, ItemCount, TotalItemSize, DeletedItemCount | Export-CSV c:\scratch\report.csv [enter]
If you want to sort this list, you can first pipe the output of Get-MailboxStatistics through the Sort-Object cmdlet, specifying the attribute and sort order, before you format-table:
Get-MailboxStatistics | Sort-Object DisplayName –ascending | ft DisplayName, ItemCount, TotalItemSize, DeletedItemCount | Export-CSV c:\scratch\report.csv [enter]
Since the normal thing to do with a CSV is massage it in Excel, I usually don’t bother with sort-object, but if you want to send out the report in an email, this could be useful.
Emailing
Speaking of sending the information out in an email, you can. Neil Hobson over at MSExchange.org wrote a great little script to do just that. Here is the script, modified slightly to keep from putting his email in easy to spider text:
###Send mailbox statistics script
###First, the administrator must change the mail message values in this section
$FromAddress = [email protected]
$ToAddress = [email protected]
$MessageSubject = “Mailbox Size Report”
$MessageBody = “Attached is the current list of mailbox sizes.”
$SendingServer = “smtp.example.com”
###Now get the stats and store in a text file
Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft
DisplayName,@{label=”TotalItemSize(KB)”;expression={$_.TotalItemSize.Value.ToKB()}},
ItemCount > mailboxes.txt
###Create the mail message and add the statistics text file as an attachment
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress,
$MessageSubject, $MessageBody
$Attachment = New-Object Net.Mail.Attachment(“./mailboxes.txt”)
$SMTPMessage.Attachments.Add($Attachment)
###Send the message
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SMTPClient.Send($SMTPMessage)
If you want to schedule this to run, create a batch file containing the following, adjusting your paths as necessary.
PowerShell.exe -PSConsoleFile “C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.psc1″ -Command “./sendstats.ps1″
And use the Windows Scheduled Tasks applet to schedule this to run as necessary.
Difference in getting mailbox statistics using Exchange Server 2000, 2003 and 2007
One of the best things that set Exchange 2007 (and 2010) apart from its predecessors is the Exchange Management Shell. Built on top of PowerShell, it greatly simplifies scripting and exposes vast amounts of information to the administrator. Previous versions of Exchange could be queried to get such useful information, but it required WMI and a fairly cumbersome multi-line script to accomplish it. Take a look at this example script from MSDN.
On Error Resume Next
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
arrComputers = Array(“Server1″, “Server2″, “Server3″)
For Each strComputer In arrComputers
WScript.Echo
WScript.Echo “==========================================”
WScript.Echo “Exchange Server: ” & strComputer
WScript.Echo “==========================================”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\MicrosoftExchangeV2″)
Set colItems = objWMIService.ExecQuery(“SELECT * FROM Exchange_Mailbox”, “WQL”, wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems