RSS Subscription 167 Posts and 2,643 Comments

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!

?View Code POWERSHELL
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
Share

3 Responses to “Another Exchange Scriplet (Moving Mailboxes)”

  1. on 17 Jan 2011 at 11:46 amJason

    Great, thanks for all the information you have provided. I have been looking for something like this today and finally got it. Keep up the good work with the posts and links…thanks again!

  2. on 13 Apr 2013 at 4:27 amFaith quotes

    This is better post then your previous one. I came here from twitter sharing well i am your blog follower on twitter, This is amazing blog just want to tell you please continue your sharing with us. In this post i just want to say that #4 point is more powerfull then others. Thank

  3. on 25 Apr 2013 at 2:06 ammusafir12

    I love to surf and my initial source for information is the blogs which have always helped me in my education. This blog is one of them.
    cute quotes

Trackback this post | Feed on Comments to this post

Leave a Reply