Exchange 2010 - EvacuateMailboxDatabase.ps1

Hey all,

This is just a simple PowerShell script I worked on for a customer in a case and I thought it might be useful to others out there. Basically, we needed to "evacuate" some bad databases - we wanted to move all the mailboxes out of the bad databases and spread them evenly across the rest of the databases. This script doesn't take into account mailbox sizes or item counts... it's just a simple round-robin reassignment of mailboxes from one database to the rest of the databases (with exclusions).

  # list of ineligible databases
 $badDBs = @("DAG01-01", "DAG01-06", "DAG01-11", "DAG01-16", "DAG01-20")
 
 # list of eligible databases
 $goodDBs = Get-MailboxDatabase | where {$badDBs -notcontains $_.name}
 
 $refugeeMailboxes = Get-Mailbox -Database "DAG01-01" # this is the database we're evacuating
 
 # the counter to track which database we're on
 $index = 0
 
 foreach ($mbx in $refugeeMailboxes) {
 $index++
 $index = $index % $goodDBs.count
 $string = "New-MoveRequest '" + $($mbx.name) + "' -TargetDatabase '" + ($goodDBs[$index]).name + "'"
 $string | Out-File -filepath 'c:\temp\commands.txt' -Append
 }

For safety, this script only generates an output text file with commands to run through PowerShell. You should validate the commands before running them. Hope this helps someone out there! :)

Best,

Matt