Migrating VMs Between ESXi Hosts with PowerCLI

Moving virtual machines between ESXi hosts is a routine VMware vSphere task, but repeatable automation makes it safer at scale. PowerCLI lets administrators discover hosts, validate placement, schedule vMotion operations and record results without relying on repeated actions in the vSphere Client.

For Australian IT teams, this is useful during maintenance windows in Sydney, Melbourne, Brisbane and regional data centres where staffing may be limited outside business hours. A controlled script can reduce disruption while supporting change-management requirements common in larger enterprises and managed service providers.

Check The vSphere Environment First

PowerCLI connects to vCenter Server rather than treating each ESXi host as an isolated target. Confirm that the account has the required privileges, then establish a secure session:

Connect-VIServer -Server vcsa01.example.com

Before moving anything, inspect the available hosts and the VM’s current placement:

$vm = Get-VM -Name "APP01"
$source = $vm.VMHost
Get-VMHost | Select-Object Name, ConnectionState, Version

The destination host should be connected, responsive and able to run the guest operating system. Verify CPU compatibility, available memory, licensing, datastore access and port group configuration. A successful vMotion depends on the same practical prerequisites as a manual migration.

Shared storage is normally required for a compute-only migration. If the VM’s files must also move, use Storage vMotion or a combined migration workflow. Confirm that the destination can access every datastore used by the VM, including secondary virtual disks and ISO locations.

Validate Compatibility And Networking

CPU generations can prevent a live migration even when both hosts appear healthy. Enhanced vMotion Compatibility, or EVC, helps present a consistent CPU feature set across hosts. This is especially important when replacing older hardware with newer servers, such as moving workloads from an older Melbourne cluster to a recently refreshed one.

Network names and VLAN-backed port groups must also match. A VM can arrive on the destination host while losing connectivity if its configured port group is absent or mapped differently. Review virtual switches, distributed switches, uplinks and physical network configuration before starting a batch migration.

For environments using NSX, inspect overlay segments and edge dependencies as part of the change. A useful related reference is this NSX high availability guide, particularly when host maintenance intersects with network availability or edge appliance placement.

Use PowerCLI To Move A VM

Once the prerequisites are confirmed, a basic host migration can be performed with Move-VM:

$destination = Get-VMHost -Name "esx02.example.com"

Move-VM -VM $vm `
        -Destination $destination `
        -RunAsync

The -RunAsync parameter returns a task object, allowing the script to continue monitoring rather than waiting without visibility. For a controlled operation, capture the task and inspect its state:

$task = Move-VM -VM $vm -Destination $destination -RunAsync
Wait-Task -Task $task

For a dry run, use -WhatIf where supported by the cmdlet and validate the resulting placement before executing the real migration. Start with one low-risk workload, then expand to groups of VMs after confirming performance, application connectivity and monitoring status.

When several VMs need to move, avoid launching an uncontrolled surge of concurrent migrations. Use a small queue, account for vMotion network capacity and consider application dependencies. Domain controllers, database servers and clustered applications may need an agreed order rather than simple alphabetical processing.

Monitor Tasks And Handle Failures

A migration script should provide useful output for an operator. Record the VM name, source host, destination host, start time, task state and any exception message. This supports audit records and helps an administrator in Perth or Adelaide review what happened after an overnight maintenance window.

A simple error-handling pattern looks like this:

try {
    $task = Move-VM -VM $vm -Destination $destination -RunAsync -ErrorAction Stop
    Wait-Task -Task $task -ErrorAction Stop
    Write-Host "$($vm.Name) migrated to $($destination.Name)"
}
catch {
    Write-Warning "Migration failed for $($vm.Name): $($_.Exception.Message)"
}

Common failures include insufficient destination memory, incompatible CPU features, missing networks, locked files and unavailable datastores. A VM with active snapshots, high memory demand or heavy disk activity may also take longer than expected. Check vCenter events and ESXi logs rather than repeatedly rerunning the same command.

For broader operational context, review this VM migration guide alongside the PowerCLI workflow. It can help connect scripted host moves with planning, validation and post-migration checks.

Build A Repeatable Migration Workflow

A reusable script should accept VM and destination names as parameters, validate that the destination is different from the source, and stop early when prerequisites are not met. It should also verify that the target host is in maintenance-safe condition and that the VM is powered on if a live vMotion is expected.

Useful operational practices include:

PowerCLI is most effective when it complements existing vCenter alarms, DRS rules and backup processes. In the Australian market, where organisations may combine on-premises VMware with colocation, cloud-connected sites and lean infrastructure teams, predictable automation reduces reliance on manual console work.

Use a small, well-documented script as the starting point for your next ESXi maintenance window. Validate one VM, review the task output, and then extend the workflow to larger migration batches with appropriate change approval and rollback planning.