The Exchange 2007 Wiki

Move Mailboxes to Target Mailbox Database

Description
This script takes in a CSV file and uses the aliases to move associated mailboxes to a specified mailbox database.
Usage


Copy the script code below into a text file and name it MoveMailboxes.ps1. Populate a csv file with aliases of the users whose mailboxes you would like to move. In the Exchange Management Shell navigate to the directory in which you saved the script and run the command below:

MoveMailboxes.ps1 aliases.csv -TargetDatabase "mailbox database name"

Sample Script


$mailbox = $null
$database = $null
$existingMailboxes = @()
$nonexistingAliases = @()

if ($args.Length -lt 3)
{
 write-host "Usage: MoveMailboxes.ps1 aliases.csv [-TargetDatabase [<>]]"
 return
}

## Check if the given target database exists
$targetdatabase = $args[2] -replace '"',""
$database = get-mailboxdatabase $targetdatabase
if ($database -eq $null)
{
 write-host "The target database doesn't exist:" $targetdatabase
 return
}

$data= import-csv -path $args[0]

## Check if there is a mailbox associated with the given alias
foreach( $i in $data )
{
 $mailbox = get-mailbox | where {$_.alias -eq $i.alias}
 if ($mailbox -eq $null) {
  $nonexistingAliases += $i.alias
 } else {
  $existingMailboxes += $mailbox
 }
}

write-host "No mailbox associated with below aliases: "
$nonexistingAliases

write-host "Move below mailboxes to the target database:" $targetdatabase
$existingMailboxes
$existingMailboxes | Move-Mailbox -TargetDatabase $targetdatabase

 

Disclaimer: The sample scripts are meant to serve as examples and may need modifications before they will work in your environment. The authors of the script are not responsible for any negative outcome that may result from using them. 

Comments

From crazy907 - 2/26/08 10:13 AM

Is it possible to move more than one mailbox at a time with this script?  I would like to set the maxthreads to 8 but can't get it to move more then 1 at a time.

 Thanks

From madgeek - 9/25/07 7:22 AM

Like desireechance, I'm looking to make this work across forests.  I've made some headway with the following but still no luck mass moving mailboxes.

 1.  Preppended the source domain to the usernames in aliasies.csv.  So 'john' becomes 'sourcedomain\john'.

2.  Edited $mailbox = get-mailbox -Identity $i.FN to read $mailbox = get-mailbox -Identity $i.FN -domaincontroller sourceDC.

I can successfully move a single mailbox using:

Move-Mailbox -TargetDatabase "Targetserver\First Storage Group\Mailbox Database" -Identity username -GlobalCatalog GCserver -SourceForestGlobalCatalog SourceGCServer -NTAccountOU "DC=xxx,DC=xxx,DC=xxx" -SourceForestCredential $SourceCredential -TargetForestCredential $TargetCredential

I set $SourceCredential and $TargetCredential when I first open Exchange Management Shell, before running the above command.

 

 Update 3:33PM:

OK, I figured out a way to do this across forests.

get-mailbox -DomainController Exchange2003DC -Credential $SourceCredential | move-mailbox -TargetDatabase "Exch2007\First Storage Group\Mailbox Database" -SourceForestGlobalCatalog SourceDC -GlobalCatalog nike -DomainController Exch2007DC -NTAccountOU "DC=xxxx,DC=xx,DC=xxx" -IgnorePolicyMatch -SourceForestCredential $SourceCredtial -TargetForestCredential $TargetCredential

Obviously you have to set $SourceCredential and $TargetCredential first.  I've only tried this using the -ValidateOnly switch so far but it appears to work. 

From thelectronichild - 6/29/07 1:08 PM

have not moved across forests however did tweak it to pull using Full Names...sample script to export user by fullname from AD below... 

On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.CommandText = _
    "SELECT Name FROM 'LDAP://OU=whicheverpathyouwishtosearchin,DC=yourcompany,DC=com' WHERE objectCategory='user'" 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    Wscript.Echo objRecordSet.Fields("Name").Value
    objRecordSet.MoveNext
Loop

------------------------ 

 now to xfer mailboxes over from powershell...

$mailbox = $null
$database = $null
$existingMailboxes = @()
$nonexistingAliases = @()

if ($args.Length -lt 3)
{
 write-host "Usage: MoveMailboxes.msh aliases.csv [-TargetDatabase [<>]]"
 return
}

## Check if the given target database exists
$targetdatabase = $args[2] -replace '"',""
write-host "Target database:" $targetdatabase
$database = get-mailboxdatabase $targetdatabase
if ($database -eq $null)
{
 write-host "The target database doesn't exist:" $targetdatabase
 return
}

$data= import-csv -path $args[0]

## Check if there is a mailbox associated with the given alias
foreach( $i in $data )
{
# $mailbox = get-mailbox | where {$_.alias -eq $i.alias}
 $mailbox = get-mailbox -Identity $i.FN
 if ($mailbox -eq $null) {
  $nonexistingAliases += $i.alias
 } else {
  $existingMailboxes += $mailbox
 }
}

write-host "No mailbox associated with below aliases: "
$nonexistingAliases

write-host "Move below mailboxes to the target database:" $targetdatabase
$existingMailboxes
$existingMailboxes | Move-Mailbox -TargetDatabase $targetdatabase

From desireechance - 6/28/07 1:14 PM

I have been playing with this script to see if I can get this to work when moving mailboxes from an Exchange 2003 organization, across forests to an Exchange 2007 organization.  any ideas?

From EvanD - 6/19/07 8:41 PM

Theelectronicchild - if you've updated the script to get it working with E2k7 RTM in your environment, please feel free to update the changes into this wiki page. That's exactly what this site is all about and we all appreciate your contributions! Smile

From thelectronichild - 6/19/07 3:23 PM

I have noticed that .msh is the old file format - If you end up with the exchange shell telling you MoveMailbox.msh is not recognized as a cmdlet, function...(yadda yadda) change the extension to .ps1 - after some tweaking to the script above it works wonders in mass mailbox moves, thanks for posting

Site

Changes
Index
Search

 

User

 

Log In
Register

 
 

Last Modified 2/8/08 6:59 AM