Script to return smallest Exchange MBX DB size


I had a client ask me to create a mailbox creation script that would create new mailboxes on the smallest DB in the US.
After doing a bit of research and running into some issue with the Get-MailboxDatabase cmdlet returining 0 for the DatabaseSize property I came up with the following script.
# This script will return the smallest mailbox database, which can be useful when creating new mailboxes or re-distributing based on size
# Created by Jason Sherry (izzy@izzy.org) 3/25/2012
# Last Updated: 6/16/2012
# Source: http://izzy.org/scripts/Exchange/Admin/Get-SmallestDB.ps1
#	 Partial code source: http://powershell.com/cs/media/p/3981.aspx

$MBXDbs = Get-MailboxDatabase | Where-Object {$_.Identity -notlike "TCN-EU-*"} # This will exclude any DB that started with "TCN-EU-"

# Loop through each of the MBXDbs

ForEach ($MBXDB in $MBXDbs)	{
	# Get current mailboxes sizes by summing the size of all mailboxes and "Deleted Items" in the database
	$TotalItemSize = Get-MailboxStatistics -Database $MBXDB | %{$_.TotalItemSize.Value.ToMB()} | Measure-Object -sum
	$TotalDeletedItemSize = Get-MailboxStatistics -Database $MBXDB.DistinguishedName | %{$_.TotalDeletedItemSize.Value.ToMB()} | Measure-Object -sum
	#Get-MailboxDatabase and the DatabaseSize property was not used since it does not return a value in some environments  as of 2010 SP2
	
	$TotalDBSize = $TotalItemSize.Sum + $TotalDeletedItemSize.Sum
	# Compare the sizes to find the smallest DB
	If (($TotalDBSize -lt $SmallestDBsize) -or ($SmallestDBsize -eq $null))
		{
		$SmallestDBsize = $TotalDBSize
		$SmallestDB = $MBXDB
		}
	}

Write-host "Smallest DB: " $SmallestDB
This entry was posted in Exchange, Microsoft, Technical and tagged , , . Bookmark the permalink.

3 Responses to Script to return smallest Exchange MBX DB size

  1. Vlad says:

    I use following
    $mbxDB_smallest = Get-MailboxDatabase * -Status | sort -Property DatabaseSize | select -First 1
    $mbxDB_smallest

    Like

  2. Dominik says:

    There is an obvious error:
    $SmallestDBsize = $DBsize

    Right:
    $SmallestDBsize = $TotalDBSize

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s