Get-Mailboxinfo Powershell script

My CEO asked me for a report about mailbox usage in the company I work for, so I thought this has to be a job for powershell.

So I sat down and tried to figure out how to get info from Exchange, I know there is a WMI class that I can query about information about the different mailboxes, so I thought I would give it a try to write a script to output things like Number of items in each mailbox, the size, deleted size etc.
I stumbled on a script by Michael B. Smith, who had made a similar script, and really done some nice work on the output of the script, so I decided not to reinvent the wheel and use his formatting.

[sourcecode lang=’posh’]
# Get-MailBoxSize.ps1
# Claus T N 2009
# This script connects to an Exchange server(s) and returns all mailbox sizes
# number of items, plus a summary.
# All formatting is “stolen” from a script by Michael B. Smith

$day = Get-Date -UFormat “%Y%m%d”
#File to output results to
$OutPutFile = “c:\scripts\MailBoxInfo-$day.txt”

[System.Int64]$script:totalSize = 0
[System.Int64]$script:totalItems = 0
[System.Int64]$script:totalDeleted = 0
[System.Int64]$script:totalMailboxes = 0

# Gets data through WMI from specified Exchange mailbox servers
$ExchangeServers = “dksrv01”

Function Get-MailboxInfo {
foreach ($computer in $ExchangeServers) {
$Mailboxes = Get-Wmiobject -namespace root\MicrosoftExchangeV2 -class Exchange_Mailbox -computer $computer | sort-object Size `

Foreach ($mailbox in $mailboxes){
If ($mailbox.MailBoxDisplayName -like “SMTP (*” -or $mailbox.MailBoxDisplayName -like “SystemMailbox*”){
else {
[string]$str = ([string]$Mailbox.MailboxDisplayname).PadRight(40)
$str += ([System.Int64]$Mailbox.Size).ToString(“N0″).PadLeft(12)
$str += ” KB”
$str += ([System.Int32]$MailBox.TotalItems).ToString(“N0″).PadLeft(9)
$str += ” ”
$str += ([System.Int64]$Mailbox.DeletedMessageSizeExtended).ToString(“N0″).PadLeft(12)
$str += ” KB”
$str | Out-File $OutPutFile -append
#”———————————————————————————-” | Out-File $OutPutFile -append

$script:totalSize += ([System.Int64]$mailbox.Size)
$script:totalItems += ([System.Int32]$mailbox.TotalItems)
$script:totalDeleted += ([System.Int64]$mailbox.DeletedMessageSizeExtended)
$script:totalMailboxes += 1

“Mailbox name Mailbox size Item count Deleted size” | Out-File $OutPutFile -append
“==================================================================================” | Out-File $OutPutFile -append

“==================================================================================” | Out-File $OutPutFile -append
$string = ($script:totalMailboxes.ToString(“N0″) + ” mailboxes”).PadRight(40)
$string += $script:totalSize.ToString(“N0″).PadLeft(12) + ” KB”
$string += $script:totalItems.ToString(“N0″).PadLeft(9) + ” ”
$string += $script:totalDeleted.ToString(“N0″).PadLeft(12) + ” KB”
$string | Out-File $OutPutFile -append
“==================================================================================” | Out-File $OutPutFile -append