PowerShell is one of the most widely-used scripting languages by Windows Administrators. All server roles and features ship with PowerShell cmdlets that admins can use to gain access to information on Windows computers.
The Hyper-V role also ships with a lot of PowerShell cmdlets. While there are a wide variety of PowerShell cmdlets available for the Hyper-V role, in this article we’ll be focusing on the Move-VM, Move-VMStorage and Compare-VM PowerShell cmdlets.
You can use this cmdlet to move a Hyper-V virtual machine along with the virtual machine storage from one Hyper-V host to another Hyper-V host.
There are several parameters supported by the Move-VM PowerShell cmdlet, but the ones that you’re likely to use the most are -IncludeStorage, -DestinationStoragePath and -VirtualMachinePath as highlighted in the commands that follow.
To move a virtual machine to a Hyper-V host named Hyper-VHost2, execute the below command:
Move-VM "MyVM1" Hyper-VHost2
Note that the above command only moves a virtual machine. In other words, it doesn’t move the storage of the virtual machine in question. If you need to move a virtual machine as well as the storage of virtual machine to a remote Hyper-V host, use this command instead:
Move-VM "MyVM1" Hyper-VHost2 -IncludeStorage -DestinationStoragePath E:MyVM1
In the above command, the -IncludeStorage parameter takes care of moving the virtual machine storage to the E:MyVM1 location on a remote Hyper-V host named Hyper-VHost2. If the virtual machine being moved requires some adjustments on the destination Hyper-V host, and if you want to make sure the move operation is successful, add the -CompatibilityReport parameter as shown in the command below:
Move-VM "MyVM1" Hyper-VHost2 -IncludeStorage -DestinationStoragePath E:MyVM1 -CompatibilityReport
The -CompatibilityReport parameter helps you understand any adjustments you might need to do on a destination Hyper-V host before the actual move takes place. This is a handy parameter that helps you avoid any operational issues during the move operation.
The is replaced with the PowerShell variable that contains the compatibility report that you generated using the Compare-VM PowerShell cmdlet.
Another parameter you can use with Move-VM is -RetainVHDCopiesOnSource. As the name suggests, it changes the move operation to a copy operation.
If you use this parameter in a Move-VM command, virtual hard disks for the virtual machine will not be deleted from the source Hyper-V host. You might want to use this parameter if you want to return copies of virtual hard disk files on the source Hyper-V host for some purposes.
Note that the Move-VM PowerShell cmdlet helps you move a virtual machine with or without virtual machine storage to a remote Hyper-V host. The Move-VMStorage PowerShell cmdlet can be used if you want to move only virtual machine storage and if the storage is only on the local Hyper-V host. Using Move-VMStorage may be easier to understand by showing some examples.
The below command moves the storage of “MyVM1” to the E:MyVMStorage location on the same Hyper-V host.
Move-VMStorage "MyVM1" -DestinationStoragePath E:MyVMStorage
Can I specify alternate paths for virtual machine files?
When using either the Move-VM or the Move-VMStorage PowerShell cmdlets, you can specify the path for storing snapshot files, smart paging file, virtual hard disk files, and virtual machine configuration files by using the parameters listed below:
- -SnapShotFilePath – Allows you to specify the new path for any snapshot files that are associated with the virtual machine.
- -VirtualMachinePath – Use this if you want to store the virtual machine configuration file (XML) to an alternate location on the Hyper-V host.
- -DestinationStoragePath – Allows you to copy virtual hard disk files to an alternate path on the Hyper-V host.
- -SmartPagingFile – As the name suggests, use this parameter to allow Move-VMStorage to move the paging file of virtual machines to an alternate location on the Hyper-V host.
For example, to move virtual machine files to different locations, use the following command with the parameters listed above:
Move-VMStorage "MyVM1" -VirtualMachinePath E:MyVM1VMConfigFile -SnapshotFilePath E:MyVM1SnapShotFiles -SmartPagingFilePath E:MyVM1SmartPaging
Another important Hyper-V PowerShell cmdlet you can use to compare virtual machines is Compare-VM. Compare-VM helps you compare a virtual machine compatibility with a Hyper-V host. This is a very useful cmdlet that you can use before moving a virtual machine to a remote Hyper-V host.
When you try to import a virtual machine using the Import-VM PowerShell cmdlet or if you move a virtual machine to a remote Hyper-V host, you might get hit with configuration errors. Compare-VM helps you generate a compatibility report and fix any configuration issues you run into. For example, to generate a compatiliby report, execute the command below:
$Report = Compare-VM -DestinationHost Hyper-VHost2 -DestinationStoragePath E:MyVM1 -IncludeStorage
This command records the compatibility report in a PowerShell variable called $Report. To display the report, use the following command:
$Report.Incompatibilities | FT -AutoSize
Nirmal Sharma is a MCSEx3, MCITP and Microsoft MVP in Directory Services. He has specialized in Microsoft Technologies since 1994 and has followed the progression of Microsoft Operating System and software. In his spare time, he likes to help others and share some of his knowledge by writing tips and articles on various sites and contributing to Solution IDs for www.Dynamic-SpotAction.com. Nirmal can be reached at firstname.lastname@example.org.