How to Import Hyper-V Virtual Machines from a Backup
In my previous article, I explained how to use the Hyper-V export feature to backup virtual machines. Naturally there may come a time when you need to import them again, perhaps to recover from some misconfiguration, data loss or even rebuilding a computer. As with exporting there are two approaches you can take.
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.
Say Goodbye to Traditional PC Lifecycle Management
Traditional IT tools, including Microsoft SCCM, Ghost Solution Suite, and KACE, often require considerable custom configurations by T3 technicians (an expensive and often elusive IT resource) to enable management of a hybrid onsite + remote workforce. In many cases, even with the best resources, organizations are finding that these on-premise tools simply cannot support remote endpoints consistently and reliably due to infrastructure limitations.
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.
PS C:\> Import-VM -Path $vmconfig -copy -VhdDestinationPath c:\Work\Ubuntu12
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.