Convert Exchange Mailboxes to Mail Enabled Remote users

This script was used as part of Exchange 2010 migration to Office 365.  No hybrid was used and mailboxes had already been created in 365.  This script creates another powershell script that can be examined and executed.  The Powershell script removes the on premise mailbox, takes note of the SMTP addresses and re-creates as a remote user.  To run this in manageable batches set the Exchange CutomAttribute1 field to “migrate” no quotes.

$mailboxes = Get-mailbox | where {$_.CustomAttribute1 -eq "migrate"}
  $file = "c:\temp\run.ps1"

out-file $file

foreach ($mailbox in $mailboxes) {

$alias = $mailbox.alias
  $samaccountname = $mailbox.samaccountname
  $emailaddresses = $mailbox.emailaddresses -split ","
  $primarysmtpaddress = $mailbox.primarysmtpaddress

"disable-mailbox $alias -Confirm:`$true" | out-File $file -append

foreach ($emailaddress in $emailaddresses) {

if ($emailaddress.contains("companyabc.onmicrosoft.com") ) {

$email = ($emailaddress.replace("smtp:","")).replace("SMTP:","")
  "enable-remotemailbox $samaccountname -primarySmtpAddress $primarysmtpaddress -remoteroutingaddress $email" | out-File $file -append

}

}

foreach ($emailaddress in $emailaddresses) {

if ( $emailaddress.toupper().contains("SMTP:") -and !$emailaddress.contains("onmicrosoft.com") ){

$email = ($emailaddress.replace("smtp:","")).replace("SMTP:","")
  "set-remotemailbox $samaccountname -emailaddresses @{add='$email'}" | out-File $file -append
  }
  }

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s