Networknet archive Blog

PowerShell: New-MigrationBatch for Exchange Online migration

Originally published

The following PowerShell script helped me and the IT team initiate mailbox migrations from Exchange to Office 365 Exchange Online. I had a couple of challenges to resolve before the script worked in our environment. We used a proxy server, so I needed to pass Proxy.Credentials to use PowerShell through it. Using the migration endpoint in our Exchange environment was another challenge because not all team members were domain admins.

The New-MigrationBatch cmdlet initiates the migration of an on-premises mailbox. The migration can then be completed with Complete-MigrationBatch. The example includes both automatic and manual completion, so choose the method you want to use before running the script. It saved the team a lot of clicking.

PowerShell32 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#Define user for the migration
$upnuser = "[email protected]"

#Define admin email
$emailadmins = "[email protected]"

#Create and define CSV file as input for New-MigrationBatch
$csvfile = '.\ExchangeOnlineOnboarding.csv'
if (Test-Path $csvfile) {  Remove-Item $csvfile}
Add-Content -Path .\ExchangeOnlineOnboarding.csv  -Value 'EmailAddress,MailboxType'
Add-Content -Path .\ExchangeOnlineOnboarding.csv  -Value "$upnuser,"

#Set proxy authentication if filtering like McAfee Web Gateway is being used
$Wcl = new-object System.Net.WebClient
$Wcl.Headers.Add(“user-agent”, “PowerShell Script”)
$Wcl.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

$UserCredential = Get-Credential -UserName $emailadmins -Message "Enter admin credentials"
$proxysettings = New-PSSessionOption -ProxyAccessType IEConfig
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection -SessionOption $proxysettings
Import-PSSession $Session -DisableNameChecking

#This migration batch will Autostart and Autocomplete for 100%. When completed the user will be migrated to Exchange Online
New-MigrationBatch -Name $upnuser -SourceEndpoint migration.networknet.nl -CSVData ([System.IO.File]::ReadAllBytes("$csvfile")) -AutoStart -BadItemLimit "1000" -TargetDeliveryDomain networknet.mail.onmicrosoft.com -AutoComplete -NotificationEmails $emailadmins

#This migration batch will Autostart and sync for 95%. You will need to run the Complete-Migration step to finish the final 5%
New-MigrationBatch -Name $upnuser -SourceEndpoint migration.networknet.nl -CSVData ([System.IO.File]::ReadAllBytes("$csvfile")) -AutoStart -BadItemLimit "1000" -TargetDeliveryDomain networknet.mail.onmicrosoft.com -NotificationEmails $emailadmins
Get-MigrationBatch -Identity $upnuser | Format-List
Complete-MigrationBatch -Identity $upnuser

#Check the migration report
Get-MigrationUserStatistics -Identity $upnuser -IncludeReport  | FL *Skip*,Error,Report