Another Exchange Scriplet (Moving Mailboxes)
This one I created for the following reasons:
1. Mailbox Database was filling up
2. Wanted to take the filled up database and split them across two different databases.
The script will sort all the mailboxes by size and take 40% of the largest mailboxes and move them to a specified database and take the remaining 60% and move them to the other database.
If you don’t want to specify the exact database name and just want PowerShell to use part of the name you specify to find a matching database, you can change the part where it states {$_.Name -eq $sourceDB} to have -like instead of -eq. Do the same for the following two pieces of code: {$_.Name -eq $targetDBLarge} and {$_.Name -eq $targetDBSmall}. -eq needs the exact match for the database name and -like basically takes the database name and puts wild cards around it so it’s more like *databaseyouspecify* and then finds a matching database that is like the name you specified.
Important: I added -whatif switches to the move-mailbox commands below to be extra safe. To actually move the mailboxes, you’ll need to remove the -whatif. And as always, MAKE SURE YOU TEST THIS IN LAB BEFORE RUNNING IT IN PRODUCTION AND HAVE A GOOD BACKUP UP AD/EXCHANGE/ETC AS WELL!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $sourceDB = "Specify your Source Database Here" $targetDBLarge = "Specify your larger database in which 40% of the largest mailboxes will be moved to" $targetDBSmall = "Specify your smaller database in which the remaining 60% of the smaller mailboxes will be moved to" ############################################ ####### Don't modify below this line ####### ############################################ $a = Get-MailboxStatistics -Database $(Get-Mailboxdatabase | Where-Object {$_.Name -eq $sourceDB}) | where {$_.ObjectClass -NotMatch '(SystemAttendantMailbox|ExOleDbSystemMailbox)'} | Sort-Object Totalitemsize $rounded = [math]::round($a.count * .4) $first = $rounded $last = $a.count - $first $a | Select-object -First $first | Move-Mailbox -TargetDatabase $(Get-Mailboxdatabase | Where-Object {$_.Name -eq $targetDBLarge}) -whatif $a | Select-object -Last $last | Move-Mailbox -TargetDatabase $(Get-Mailboxdatabase | Where-Object {$_.Name -eq $targetDBSmall}) -whatif |
Elan Shudnow :: May.19.2009 :: Exchange, PowerShell ::
