Exchange 2007 Mailbox Statistics

If only Microsoft would realize that it’s pretty easy to add a single column known as “size of mailbox” when searching through the GUI exchange management tool. Instead, we have to open up the Command prompt tool. Big deal.

Here’s the quick and easy way to list the name of the mailbox, size of said mailbox, and number of emails:
Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft DisplayName,@{label="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount

And apparently you can get it to email you if you write a script as follows:
###Send mailbox statistics script
###First, the administrator must change the mail message values in this section
$FromAddress = "MailboxReport@ngh.net"
$ToAddress = "administrator@ngh.net"
$MessageSubject = "Mailbox Size Report"
$MessageBody = "Attached is the current list of mailbox sizes."
$SendingServer = "e2k7.ngh.net"
###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)

But I couldn’t get it to actually attach the txt document.

EDIT!!!

11.11.2008

I got it to work – the problem was with a couple issues we had.

1.) In the Windows PowerShell, you must Set-ExecutionPolicy Unrestricted
2.) I wrote a bat file to open this once a week:
C:\WINDOWS\system32\WindowsPowerShell\v1.0\PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command ". 'C:\sendstats.ps1'"
3.) I wrote a ps1 file to run the actual commands (see above for the code)
4.) If you get an error, see if the following command helps:
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
5.) MAKE SURE the get-mailboxstatistics is all on one single line – I can’t tell you how much word-wrap in notepad screwed me out of 20 minutes of time.

***EDIT 05.23.2011***
So Exchange 2010 screwed me a little bit on this – it requires another flag:
Get-MailboxStatistics -server SERVERNAME | Sort-Object TotalItemSize -Descending | ft DisplayName,@{label="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount

You can use -Identity MAILBOXORUSERNAME, -Database DATABASEHERE, or -Server SERVERNAMEHERE. I chose server as it’s exactly what I needed.

Exchange 2007 Remove Disconnected Mailboxes

If you delete a user on Exchange 2007, it will delete the user in Active Directory. It will not, however, delete the mailbox that was associated with the user. This mailbox will then move onto bigger and better things – namely just taking up storage space on your Exchange server.

Quick and easy way:
On your Exchange server, open up the Exchange Management Shell
Get-MailboxStatistics | where-object { $_.DisconnectDate -ne $null } | Select DisplayName,MailboxGuid – This will show all the disconnected mailboxes AND their associated MailboxGuid (which is needed to delete the boxes)
Remove-Mailbox -Database [Database-Name] -StoreMailboxIdentity [MailboxGuid] – This will delete a single line item.

EDIT 11.13.2008:
You can run the Get-MailboxDatabase command to find out the name of the database and what server it resides on. The final delete command will be something like the following:
Remove-Mailbox -Database "servername\mailbox database" -StoreMailboxIdentity 2ae3c6f1-848e-4892-923c-614f9b3838f7
Then it will ask if you want to really remove the GUID from the database.