PowerShell Script: Get Exchange 2007 Database Sizes and Mailbox Counts

I’ve spent a lot of time working on Exchange migration projects lately, which means that I’ve moved a lot of mailboxes. When I’m planning batches of mailboxes to migrate one of the pieces of information I look at is the current size and mailbox count for the mailbox databases on the server I’m migrating to.

For Exchange Server 2007 environments I use this script to quickly report that information to me.

[PS] C:\Scripts>.\get-mbinfo.ps1 -server EX2007MB

Database                                    Size (Mb)                 Mailboxes
--------                                    ---------                 ---------
EX2007MB_DB1                              98774.89                        86
EX2007MB_DB2                              92505.02                        53
EX2007MB_DB3                                102523                        63
EX2007MB_DB4                                107795                        61
EX2007MB_DB5                              98085.02                        72
EX2007MB_DB6                              101591.3                        69
EX2007MB_DB7                              112167.1                        90
EX2007MB_DB8                                102109                        57
EX2007MB_DB9                                104767                        35

This script is only written to work for Exchange Server 2007 Mailbox servers at the moment, and needs only one parameter provided (the -server).

Here is the full script code.

#
#.SYNOPSIS 
#Returns the list of mailbox databases on a server
#along with the mailbox count and .edb file size
#for each database.
#
#.EXAMPLE
#.\get-mbinfo.ps1 -server EX2007MB
#

#You must specify a server name
param($server)

#If server name not provided try using the local host
if ($server -eq $nul) {$server = $env:COMPUTERNAME}

#Exit if server is not an Exchange mailbox server
$rolecheck = Get-ExchangeServer $server -erroraction silentlycontinue
if ($rolecheck.IsMailboxServer -ne $true)
{
	Write-Host "Server" $server "is not a mailbox server" -foregroundcolor red -backgroundcolor white
	Write-Host "Use -Server to specify a valid mailbox" -foregroundcolor red -backgroundcolor white
	Write-Host "server to run script against." -foregroundcolor red -backgroundcolor white
	Exit
}

#Check if its a cluster so that node name can be used for EDB file size check
$cluster = get-ClusteredMailboxServerStatus -identity $server -erroraction silentlycontinue 
if ($cluster -eq $nul)
{
	$servername = $server
}
else
{
	$CluObj = New-Object -com "MSCluster.Cluster"	
	$CluObj.Open($Server)
	$ActiveNode = $CluObj.ResourceGroups.Item($Server).OwnerNode.Name

	$servername = $ActiveNode
}

#Get the list of mailbox databases from the server (excluding recovery databases)
$dbs = Get-MailboxDatabase -server $server | Where {$_.Recovery -ne $true}

#Quick reorder
$dbs = $dbs | sort-object name

foreach ($db in $dbs)
{ 
	#Get the mailbox count for the database
	$mailboxes = Get-Mailbox -database $db -IgnoreDefaultScope -Resultsize Unlimited -erroraction silentlycontinue
	$mbcount = $mailboxes.count
	
	#Get the EDB file size for the database
	$edbfilepath = $db.edbfilepath 
	$path = "`\`\" + $servername + "`\" + $db.EdbFilePath.DriveName.Remove(1).ToString() + "$"+ $db.EdbFilePath.PathName.Remove(0,2) 
	$dbsize = Get-ChildItem $path 
	[float]$size = $dbsize.Length /1024/1024 
	$dbname = $db.name

	$returnedObj = new-object PSObject
	$returnedObj | add-member NoteProperty -name "Database" -value $dbname
	$returnedObj | add-member NoteProperty -name "Size (Mb)" -value $size
	$returnedObj | Add-Member NoteProperty -Name "Mailboxes" -Value $mbcount
	$returnedObj
}

Note: I referenced some code samples on the web when I was putting this together but forgot to track where they were from. If you spot a snippet that originated from you please let me know so I can add credit.

About Paul Cunningham

Paul is a Microsoft Exchange Server MVP and publisher of Exchange Server Pro. He also holds several Microsoft certifications including for Exchange Server 2007, 2010 and 2013. Connect with Paul on Twitter and Google+.

Comments

  1. Thanks a bunch! Exactly what I was looking for.

  2. Thanks a whole load. This is just what i needed. Your articles rock Paul!

Leave a Comment

*

We are an Authorized DigiCert™ SSL Partner.
Loading...

Still running Exchange 2003? Time to get moving and start your upgrade. Find out how - Click Here