How to Import Hyper-V Virtual Machines from a Backup
Using Hyper-V Manager to Import Virtual Machines
I have covered using Hyper-V Manager a bit already. In a recent article, I discussed how to import virtual machines using Windows 8 Client Hyper-V. In short, when you import, enter the top-level folder and follow the wizard. Because everything is “self-contained” from your export it should go pretty smoothly. But I want to point out a critical choice below in Figure 1.
If you use the first option, the virtual machine will be registered in Hyper-V but all of the files will remain in the export location. Personally, I like to keep my export folders separate so I try to avoid this option. If you are restoring due to data loss or rebuilding, choosing to Restore the Virtual Machine (the second option) is best. This option will read the configuration files and import everything back to their original location. The end result is that you will be back where you started.
If you choose to Copy the Virtual Machine, it will get a new guide, but you ‘ll also be able to specify where you want the different elements to go as you can see in Figure 2.
You can also specify the location for the VHD files. See Figure 3.
Use this option when you are importing and your original Hyper-V structure has changed. However you do it, you can only import one virtual machine at a time.
Using PowerShell to Import Virtual Machines
An alternative is to use PowerShell and the Import-VM cmdlet. This tool works very similar to the GUI, although it takes a little more work. For one, you need the full path the configuration XML file. This is where tab completion comes in handy. Or you can use something a bit more complicated.
First, get the the full path to the xml file. There should only be one.
PS C:\> $vmconfig = get-item 'E:\Exports\Test Rig\Virtual Machines\*.xml' | select -ExpandProperty Fullname
Now we can use that with Import-VM.
PS C:\> Import-VM -Path $vmconfig -whatif What if: Import-VM will import the virtual machine saved at "E:\Exports\Test Rig\Virtual Machines\988E46E2-1A4C-4822-8C0D-6886DB2398BE.XML".
This cmdlet supports –Whatif, which is very useful.
Now before you jump off and start importing, be careful: The default behavior is to register the imported virtual machine in place. If that is what you want then by all means rerun the command without –Whatif. This might take a while so this cmdlet, like Export-VM, also has a –AsJob parameter to run the import in a PowerShell background job.
Personally, I prefer to include the –Copy parameter.
PS C:\> Import-VM -Path $vmconfig -copy
This will import the virtual machine, copying files. Be aware you will get errors if any existing files are still there. The files will be copied to your default locations, and NOT the original locations.
If you look at help for Import-VM, you’ll see parameters that seem to indicate you can specify locations for items like the VHD and Snapshots. But the help says they are obsolete. To be honest I’m not sure what this means because I can use the parameters and redirect items to new locations.
Using PowerShell also makes it much easier to import several machines from the same location, assuming they were exported that way. For example, I have a folder E:\Exports\2012_10_04 with several exported virtual machines.
PS C:\> dir E:\Exports\2012_10_04 | select name Name ---- JDHLab Core DC JDHLab Win7 Test Rig Ubuntu 12 x86
With a little effort, I could import all of them with code like this:
Depending on your need, you may be able to simply copy a file from the export folder and overwrite any existing files that need to be replaced. Otherwise the Hyper-V Manager is a decent place for single restores. But if you are looking to recover a complete Hyper-V library with minimal interaction, the PowerShell cmdlets are the way to go.
More in Hyper-V
How to Install Hyper-V on Windows Server Core
Mar 16, 2022 | Michael Reinders
Microsoft Says There Will Be No Hyper-V Server 2022 Free Edition
Dec 13, 2021 | Michael Reinders
How to Stop an Unresponsive Hyper-V Virtual Machine
Jun 29, 2021 | Russell Smith
How to Enable Nested Virtualization in Hyper-V
Jun 28, 2021 | Russell Smith
Disaster Recovery with Azure Backup for SQL Server VMs
May 28, 2019 | Michael Otey
Leveraging Virtualization for Improved Availability and Disaster Recovery
Apr 29, 2019 | Michael Otey
Most popular on petri