IT Blog

DevOps Technical Articles

Office 365 Migrating users from on-premise using PowerShell

[Background]

This PowerShell script will assist in automating the task of migrating On-Premise Exchange users to Office 365 when running in Hybrid deployment mode.

The script will simply automate the manual tasks done within Office 365 Exchange Admin using PowerShell.

[Pre-requisites]

You will need to have the appropriate MS Online tools installed on your machine before starting. They are:Microsoft Online Services Sign-In Assistant for IT Professionals RTW

  1. Azure Active Directory Module for Windows PowerShell Reference
  2. Download; “AdministrationConfig-V1.1.166.0-GA.msi”

[Code]

$identity = "john.doe" #user to migrate
$ExchangeURL = "hybrid.mydomain.com" # your Exchange EAC UR ges here (or wherever you have setup Hybrid access point)
$O365Domain = "mydomain.onmicrosoft.com" # your Office 365 domain tenancy goes here

Set-ExecutionPolicy Unrestricted -Force

$O365Credentials = Get-Credential
$ExchangeCredentials = Get-Credential

# e.g. DOMAIN_NAME\administrator


$SESSION = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $O365Credentials -Authentication Basic -AllowRedirection
Import-PSSession $SESSION
Connect-MsolService -Credential $O365Credentials
 

#Move the Mailbox

New-MoveRequest -Identity "$identity" -Remote -RemoteHostName $ExchangeURL -TargetDeliveryDomain O365Domain -RemoteCredential $ExchangeCredentials -BadItemLimit 1000