Move Mailbox to Office 365 with PowerShell

Refer to the steps below on how to Move Mailbox to Office 365 with PowerShell

  1. Prepare a csv file
EmailAddress
uat5@aventislab.com
uat6@aventislab.com

The header for this column must be named EmailAddress

  1. Assign Office 365 License to all users listed in the CSV file
#Assign License 
$Users = Import-Csv "C:\Temp\MailboxToMigrate.csv"
$AccountSKU = Get-MsolAccountSku | ? AccountSkuID -like "*ENTERPRISEPACK"

foreach ($User in $Users) { 

 if (Get-MsolUser -UserPrincipalName $User.UPN) {

    Set-MsolUser -UserPrincipalName $User.UPN -UsageLocation MY
    Set-MsolUserLicense -UserPrincipalName $User.UPN -AddLicenses $AccountSKU.AccountSkuId
    Write-Host -ForegroundColor Green $User.UPN" had been licensed successfully"
 } 
} 
  1. Define the Name of the Migration Batch, and the Migration Endpoint & target Delivery Domain
$Name = "Batch1-UAT56"
$SourceEndpoint = Get-MigrationEndpoint
#The target Delivery Domain is <tenantname>.mail.onmicrosoft.com
$TargetDeliveryDomain = "M365x333981.mail.onmicrosoft.com"
  1. Create a migration batch to migrate users listed in CSV file to Office 365
New-MigrationBatch -Name $Name -BadItemLimit 100 -SourceEndpoint $SourceEndpoint.Identity -AutoComplete -CSVData ([System.IO.File]::ReadAllBytes("C:\Temp\temp.csv")) `
-NotificationEmails "administrator@aventislab.com" -TargetDeliveryDomain $TargetDeliveryDomain

Identity     Status  Type               TotalCount
--------     ------  ----               ----------
Batch1-UAT56 Stopped ExchangeRemoteMove 2         
  1. Start the Migration Batch
Start-MigrationBatch $Name
  1. Monitor the Migration Batch with Get-MigrationUser & Get-MigrationUserStatistics
Get-MigrationUser

Identity                                 Batch                          Status                    LastSyncTime                                                                                     
--------                                 -----                          ------                    ------------                                                                                     
ps1@PowerShell365.com                    PS1                            Failed                                                                                                                     
UAT3@Aventislab.com                      UAT3                           Completed                 26/11/2018 5:41:02 AM                                                                            
uat5@aventislab.com                      Batch1-UAT56                   Syncing                                                                                                                    
uat6@aventislab.com                      Batch1-UAT56                   Syncing                                             

#Detail Monitoring 
$User = "uat5@aventislab.com" 
Get-MigrationUserStatistics -Identity $User | Select MailboxEmailAddress, Status, TotalItemsInSourceMailboxCount, SyncedItemCount, EstimatedTotalTransferSize, BytesTransferred

MailboxEmailAddress            : UAT5@Aventislab.com
Status                         : Syncing
TotalItemsInSourceMailboxCount : 
SyncedItemCount                : 0
EstimatedTotalTransferSize     : 
BytesTransferred               : 
  1. Force the completion of mailbox when it is stucked in SYNCED
#Force Complete for Mailbox shown in SYNCED
$mailbox="uat5@aventislab.com"
Set-MoveRequest -Identity $mailbox -CompleteAfter (Get-Date)
Resume-MoveRequest -Identity $mailbox
  1. User can login to https://portal.office365.com to access thier email once the migration batch is completed successfully

Reference
1. https://docs.microsoft.com/en-us/powershell/module/exchange/move-and-migration/new-migrationbatch?view=exchange-ps

Leave a Comment