August 19, 2014

How to Export Hyper-V Virtual Machines

​When you export a virtual machine, you get the configuration file, a copy of the VHD, and any snapshots. What is attractive about this feature is that you can move the virtual machine anywhere else or even reimport it in the event of data loss or if you rebuild a system. Before you can export a virtual machine, however, it must be shut down.

Using Hyper-V Manager to Export a Virtual Machine

There are two ways to export a virtual machine. In the Hyper-V Manager, you can select a virtual machine, then right-click and choose Export from the context menu.​



Next, you will be prompted for a folder path


Click Export to begin. However, there's one major "gotcha" -- you can't have a previously exported version in the same location.


You can delete or move the existing files and repeat. Be aware that this export will need as much space as the existing VHDs and snapshots, and it might take a while to finish. Another option is to specify another path. If the folder doesn't exist, it will be created. This would let you create date-named folders.



However you choose to do it, the virtual machine will be a top-level folder.



​It is also possible to select a number of virtual machines in Hyper-V Manager and export to the same top-level directory. Remember, the virtual machine must be turned off. Each virtual machine will get its own folder structure under the specified path.

Using PowerShell to Export a Virtual Machine

Another approach is to use PowerShell to export the virtual machine (assuming you are running Windows 8 or Windows Server 2012 and have Hyper-V installed). In the Hyper-V module, you can use the Export-VM cmdlet. You can export a VM by its name.​


PS C:\> export-vm "test rig" -Path E:\Exports
Or as a virtual machine.
PS C:\> get-vm "test rig" | export-vm -Path E:\Exports​


​The same rules apply: You can't have an existing export with the same name in the target path. This makes it easy to get a number of virtual machines and export them all at once.

PS C:\> get-vm "test*" | export-vm -path E:\Exports\2012_10_04​


​The cmdlet won't write anything to the pipeline unless you use –Passthru. Also be aware that Export-VM does NOT support –Whatif or –Confirm, although you could wrap this up in a function and add your own –Whatif as well as better error handling for things like existing files.

Because the export can take a long time, you can use the cmdlet's –AsJob parameter.

 PS C:\> get-vm jdh* | export-vm -Path E:\Exports\2012_10_04 -AsJob

Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
2 Job2 NotSt... True get-vm jdh* | ...
3 Job3 NotSt... True get-vm jdh* | ...


You get a job for every virtual machine, and you can let them run in the background while you keep working. Overall, using PowerShell is great because it means you can set up a PowerShell scheduled job or a task to perform periodic exports.

The Export feature in Hyper-V is a handy way to perform relatively fast and easy backups, especially if you export to removable drives. Yes, there are dedicated Hyper-V backup utilities, but in some situations, exporting may be all that you really need.​

No comments:

Post a Comment