If you have multiple Storage Groups / Databases on your Exchange 2007 server, you may want to try to spread your mailboxes over all databases. You could use your “gut feeling” and/or select a database at random, or you could use a simple script to select the “best” database for hosting a new mailbox.
The following script will query all databases, find the total size and number of mailboxes, and will then prompt the most optimal database based on average size per mailbox in the databases.
# # Powershell Script written by Peter Van Eeckhoutte # # Purpose : select the most optimal database for # hosting a new mailbox in Exchange 2007 # taking into account that mailbox data should # be spread across multiple databases as much as possible # # http://www.corelan.be:8800 # # # Load assembly [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") $Databases = Get-MailboxDatabase -status | sort-object Name $lowestavgsize=0 $optimaldb="" foreach ($Database in $Databases) { write-host "Database : $Database" write-host "--------------------------------------------------------------" if ($Database.Mounted -eq "True") { $dbstats = Get-MailboxStatistics -database $Database $totaldbsize = 0 $totalnrofmb = 0 $avgmbsize = 0 foreach ($dbstat in $dbstats) { $totalnrofmb++ $totaldbsize = $totaldbsize + $dbstat.TotalItemSize.Value.ToMB() } if ($totalnrofmb -gt 0) { $avgmbsize = $totaldbsize/$totalnrofmb } $avgmbsize=[math]::round($avgmbsize, 2) write-host " Total Database Size : $totaldbsize Mb" write-host " Number of mailboxes : $totalnrofmb " write-host " Average mailbox size : $avgmbsize Mb" if (($lowestavgsize -eq 0) -and ($optimaldb -eq "")) { $lowestavgsize = $avgmbsize $optimaldb = $Database } else { if ($avgmbsize -lt $lowestavgsize) { $optimaldb = $Database $lowestavgsize = $avgmbsize } } } else { write-host "Database : $Database : not mounted" write-host "--------------------------------------------------------------" } write-host "" } #find optimal database [System.Windows.Forms.MessageBox]::Show("Database :`n$optimaldb`n Avg mailbox size : $lowestavgsize Mb","Optimal Database")
You can either run this powershell directly from a Powershell command line, or you can create a batch file to run the script :
Some assumptions :
selectoptimaldb.bat
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile "D:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command ". 'C:\Scripts\selectoptimaldb.ps1'"
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
-PSConsoleFile "D:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1"
-command ". 'C:\Scripts\selectoptimaldb.ps1'"
(put everything on one line, save the file, put a shortcut on your desktop, and you’re ready to go)
© Corelan Consulting BV. All rights reserved. The contents of this page may not be reproduced, redistributed, or republished, in whole or in part, for commercial or non-commercial purposes without prior written permission from Corelan Consulting bv. See our Terms of Use & Privacy Policy (https://www.corelan.be/index.php/legal) for more details.
Subscribe to get the latest posts sent to your email.
Type your email…
Subscribe
Peter Van Eeckhoutte is the founder of Corelan and a globally recognized expert in exploit development and vulnerability research. With over two decades in IT security, he built Corelan into a respected platform for deep technical research, hands-on training, and knowledge sharing. Known for his influential exploit development tutorials, tools, and real-world training, Peter combines a strong research mindset with a passion for education—helping security professionals understand not just how exploits work, but why.
Tags: