Powershell Move Mailbox Script for Exchange 2007 and Exchange 2010

I recently had to move a group of mailboxes from one server to another, and it was going to take all weekend for it to complete. I didn't want to leave it unattended in case there were problems and it just bombed out, but I really didn't want to stay and watch it, either.


The solution was a small powershell script that sent me an email when a mailbox move was started and finished. This way I could passively track the progress and if there were any problems, I would have known if there were any problems when I didn't receive any emails.


Next time, I will probably add in some error checking and email alerts in case of failures so I'd know what to expect when I went back to see the finished process.


Note that this needs to be run from within the exchange management shell context.

$emailFrom = 'someone@yourdomain.com';
$emailTo = 'someoneelse@yourdomain.com';
$smtpServer = 'smtp.relay.yourdomain.com';
$targetDatabase = 'SOMEWHERE\Your Storage Group\Your Mailbox Database';
$globalCatalog = 'gcserver.yourdomain.com';
$domainController = 'dc.yourdomain.com';

$smtp = new-object Net.Mail.SmtpClient($smtpServer)

get-mailbox | foreach {
	$mess = "Beginning to move ";
	$mess += $_.DisplayName.ToString();
	echo $mess;
	$smtp.Send($emailFrom, $emailTo, $mess, $mess)
	move-mailbox -BadItemLimit '20' -TargetDatabase $targetDatabase -GlobalCatalog $globalCatalog -DomainController $domainController -confirm:$false;
	$mess = "Done moving ";
	$mess += $_.DisplayName.ToString();
	echo $mess;
	$smtp.Send($emailFrom, $emailTo, $mess, $mess)

Back to Article Index

blog comments powered by Disqus