WavebreakmediaMicro - Fotolia
You can import and export VMs using Hyper-V Manager or System Center Virtual Machine Manager, but it's just as easy to use PowerShell commands. Microsoft offers the necessary commands to perform both import and export operations for VMs running on Hyper-V.
The Export-VM PowerShell cmdlet is available on any computer on which the Hyper-V role is installed, but you can also install Hyper-V PowerShell modules on a management server and perform necessary Hyper-V operations. To export a single VM, execute the following command:
“Export-VM <VMName> -Path “<PathToExportVMFiles>”
Export-VM takes the necessary actions before exporting VM files, such as creating a folder on the path specified in the -Path variable, which creates three subfolders: Snapshots, Virtual Hard Disks and Virtual Machines. These subfolders contain VM files, such as the extensible markup language (XML) file, which contains VM configuration, VM snapshot and VHDX files. If you want to export all VMs running on the local Hyper-V host, run the Get-VM | Export-VM –Path <PathToExportVMFiles> command. If you want to export specific VMs, all you have to do is create a text file that contains VM names and then execute the PowerShell script below.
ForEach ($VMName in Get-Content "$VMFile")
Export-VM -Name $VMName -Path "E:\VMs\"
This will export only the VMs specified in the C:\Temp\VMs.txt file. All VMs will be exported under the E:\VMs folder.
It's important to note that Export-VM doesn't support a -ComputerName parameter, which you can use to export a VM on a remote Hyper-V host.
The Import-VM PowerShell cmdlet can help you import a VM. To import a VM, execute the command below:
Import-VM –Path “E:\VMs\VMGUID.XML” OR <E:\VMs\VMGUD.VMCX”
When it comes to importing a VM, the import process takes any necessary actions. For example, if the VM is already registered with Hyper-V, it deletes the existing VM with the same VM globally unique identifier and then registers the VM again. If you are importing the VM on a different Hyper-V host, the import process just proceeds with the registration of the VM because the VM doesn't already exist. Note that you shouldn't import multiple VMs using a PowerShell script as the import process also checks the VM's compatibility on the target Hyper-V host.
Tap into the PowerShell pipeline
Navigate Windows PowerShell with this guide
Dig Deeper on Microsoft Hyper-V management
Related Q&A from Nirmal Sharma
Use System Center Virtual Machine Manager and PowerShell to make logical network management easy. Execute a script to display network virtualization ... Continue Reading
Hyper-V replication is easy to use, but there are a number of steps you should take during Hyper-V replication setup to ensure you stay abreast of ... Continue Reading
Manage Hyper-V clusters more easily with PowerShell cmdlets and scripts that automate the retrieval and display of node information, including node ... Continue Reading