Scripting and Migrating Mailbox Moves to Exchange using Groups

You can if you wish move Mailboxes using the Exchange Management Console, or individually using the Exchange Management Shell using New-MoveRequest. You can also use Migration Batches using the New-MigrationBatch and commands.

However if you have large numbers of Mailboxes to move, a great way of accomplishing this is using Distribution Groups. You can use these to communicate with users and they also serve as a point of reference to Support Staff who can then easily find users that have moved.

The following script not only allows you to move users based on their Distribution Group, but also allows you during your migration to check basic pre-requisites before submitting a Mailbox move and because it keeps basic settings within the script, allows you to start moves (even for a single user) quickly and easily.

Usage:

.\ToCloud.ps1 -Name “Finance Users”

The above example initiates a migration of all users in the Finance Users distribution group to Exchange Online.

.\ToCloud.ps1 -Name “Steve Goodman” -Suspend

The above example initiates a migration of just Steve Goodman, and suspends the move request. You could use the suspend option to get ready a large distribution group for migration early on in the day, then resume the request in the evening to begin the actual migration.

You can check the status of a batch using the Get-MoveRequest cmdlet for example

Get-MoveRequest -BatchName "Finance Users"

Batch Names will be the Distribution Group name, or for individual users, the batch will be named “Individuals”.

param([string]$Name,[switch]$Suspend)
if (!$Name)
{
	throw "Parameter -Name is required"
}

# Your organization's settings
$HybridServer = "mail.contoso.com"
$DeliveryDomain = "contoso.mail.onmicrosoft.com"
$OnPremiseUsername = "CONTOSO\migrator"
$CloudUsername = "[email protected]"
$Password = ConvertTo-SecureString "Password" -AsPlainText -Force

$CloudCred = New-Object System.Management.Automation.PSCredential $CloudUsername, $Password
$OnPremCred = New-Object System.Management.Automation.PSCredential $OnPremiseUsername, $Password
if (!(Get-Command New-CloudMoveRequest -ErrorAction SilentlyContinue))
{
	$session = New-PSSession -ConfigurationName Microsoft.Exchange -Authentication Basic -ConnectionUri https://ps.outlook.com/powershell -AllowRedirection:$true -Credential $CloudCred
	Import-PSSession $session -Prefix Cloud
}
if (($MailUser = Get-CloudMailUser $Name -ErrorAction SilentlyContinue))
{
	if (!$MailUser.ExchangeGuid.Guid)
	{
		throw "$($Name) doesn't appear to have an Exchange GUID set"
	}
	$Move = $MailUser
	$BatchName = "Individuals"
} elseif (($DG = Get-CloudDistributionGroup $Name -ErrorAction SilentlyContinue))
{
	$DGMembers = Get-CloudDistributionGroupMember $Name
	foreach ($DGMember in $DGMembers)
	{
		if ($DGMember.RecipientType -ne "MailUser")
		{
			throw "DG Member $($DGMember.Name) is not an on-premise mailbox"
		}
		$DGMemberMailUser = $DGMember | Get-CloudMailUser
		if (!$DGMemberMailUser.ExchangeGuid.Guid)
		{
			throw "DG Member $($DGMember.Name) doesn't appear to have an Exchange GUID set"
		}
	}
	$Move = $DGMembers
	$BatchName = $Name
} else {
	throw "Invalid input. Please specify a Cloud Mail User or Distribution Group"
}
if ($Suspend)
{
	$Move| New-CloudMoveRequest -Remote -RemoteHostName $HybridServer -RemoteCredential $OnPremCred -TargetDeliveryDomain $DeliveryDomain -BatchName $BatchName -Suspend
} else {
	$Move| New-CloudMoveRequest -Remote -RemoteHostName $HybridServer -RemoteCredential $OnPremCred -TargetDeliveryDomain $DeliveryDomain -BatchName $BatchName
}
if ($session)
{
	Remove-PSSession -Session $session
}

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top