Exchange 2007 move-mailbox script for bulk move using powershell

Posted on 2008-10-05
Last Modified: 2012-08-27
We are about to move users cross forest from Exchange 2003 to Exchange 2007. We have tested the command below which works perfect for moving a single mailbox. As I am not an expert with powershell, can somebody assist with creating a script that will do a bulk move of mailboxes based on a txt file with users aliases (or sam account).

This is what we have completed for 1 mailbox:
$SourceCredential = Get-Credential  (This is the username and password for source domain)
$TargetCredential = Get-Credential (This is the username and password for target domain)

Move-Mailbox -TargetDatabase "EX07\First Storage Group\Small Mailboxes" -Identity eric_sdaly -GlobalCatalog dc01.targetdomain.local -SourceForestGlobalCatalog SourceDomainController -NTAccountOU "OU=Secure,OU=Ireland,OU=User Accounts/Groups,DC=targetdomain,DC=local" -SourceForestCredential $SourceCredential -TargetForestCredential $TargetCredential -SourceMailboxCleanupOptions MailEnableSourceAccount

Hope someone can help on this.
Question by:ericdaly
LVL 70

Accepted Solution

Chris Dent earned 250 total points
ID: 22648453


That should be quite easy actually :)

PowerShell has a nice CSV File Reader that can be used to read the input file, we could just loop through using that.

Imagine your text file (call it users.csv for this example) looked like this:


The column header is important, with that we can do:

$UsersCSV = Import-CSV -Path "users.csv"

Which will let us loop through each like this:

$UsersCSV = Import-CSV -Path "users.csv"
foreach ($Line in $UsersCSV) {
    Move-Mailbox -TargetDatabase "EX07\First Storage Group\Small Mailboxes" `
        -Identity $Line.Username -GlobalCatalog dc01.targetdomain.local `
        -SourceForestGlobalCatalog -SourceDomainController `
        -NTAccountOU "OU=Secure,OU=Ireland,OU=User Accounts/Groups,DC=targetdomain,DC=local" `
        -SourceForestCredential $SourceCredential -TargetForestCredential $TargetCredential `
        -SourceMailboxCleanupOptions MailEnableSourceAccount

We can access each line with the ForEach loop, then we get the Username column with "$Line.Username". The ` used above just allows us to split the command over several lines, tends to make it easier on the eyes.


LVL 11

Assisted Solution

Sudhirchauhan3 earned 250 total points
ID: 22648454
This is what you need. $users = get-content inputfile.txt  will read the aliases or samaccountname from the inputfile.txt . For each loop will iterate through the list and move the mailboxes.


$SourceCredential = Get-Credential  (This is the username and password for source domain)
$TargetCredential = Get-Credential (This is the username and password for target domain)
$users = get-content inputfile.txt
foreach ( $user in $users ) {
Move-Mailbox -TargetDatabase "EX07\First Storage Group\Small Mailboxes" -Identity $user -GlobalCatalog dc01.targetdomain.local -SourceForestGlobalCatalog SourceDomainController -NTAccountOU "OU=Secure,OU=Ireland,OU=User Accounts/Groups,DC=targetdomain,DC=local" -SourceForestCredential $SourceCredential -TargetForestCredential $TargetCredential -SourceMailboxCleanupOptions MailEnableSourceAccount
if ($? -ne True ) { 
write-host "Move-Mailbox Failed for user $user ."

Open in new window


Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
How to resolve IMCEAEX NDRs in Exchange or Exchange Online related to invalid X500 addresses.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question