PowerShell: Live migration of all VMs from one host to another

Simple script that uses live migration to move all VMs from one Hyper-V host to another. I’m not using shared storage so have added the -IncludeStorage option to the end of the Move-VM command.

You should obviously have previously enabled live migration properly for your hosts before running this script.

$SourceHost = "HyperV01"
$DestinationHost = "HyperV02"
$VMsOnHost = Get-VM -ComputerName $SourceHost

foreach ($VM in $VMsOnHost){
   Write-Host ("Moving "+$VM.Name)
   Move-VM -ComputerName $SourceHost -Name $VM.Name -DestinationHost $DestinationHost -IncludeStorage
}

Note that the above will assume that the path to the VM configuration and disk files is the same on both the source and destination hosts, e.g. D:\VMs

This entry was posted in Hyper-V, PowerShell and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s