Export Hyper-V Configuration Using PowerShell

Posted on November 13, 2014 by Jeff Hicks in Hyper-V with 0 Comments

When Windows 8 and Windows Server 2012 were released, we also received a new PowerShell module. Within this module are many cmdlets that are designed to make it easy to manage Hyper-V hosts and virtual machines directly from PowerShell. Many of these cmdlets and command line versions of functionality that exists within the graphical Hyper-V manager. But sometimes, even these cmdlets may not meet your needs. As a case in point, consider the Export-VM cmdlet. This cmdlet will export a virtual machine to disk including its disk files and snapshots. In other words, a backup.

Using PowerShell to Export a Hyper-V Configuration

I’m assuming that if you are running Hyper-V in a production environment, then you probably have invested in a backup solution. What I want to demonstrate in this article isn’t intended to replace those products, but rather supplement them. If you run a smaller shop, a lab environment, or client Hyper-V on a Windows 8 or later desktop, then this article may be especially handy.

The problem is that when you use the Export-VM cmdlet, you get everything and given the size of the virtual machine hard drives and snapshots, this process may take some time to complete. But perhaps you only want to export the configuration itself? I was working on this problem when I came across someone with this exact issue. He wanted to export the virtual machine configuration so that he could import it later.

The configuration that you see when you run Get-VM or look at a virtual machine settings in Hyper-V Manager are stored in an XML file. The file location is included in the virtual machine object.

The name of the file is the same as the virtual machine’s id.

It is pretty easy to get that file with PowerShell.

The file should exist in a sub-folder called Virtual Machines, but sometimes it is not. There is also a possibility there might be multiple XML files, so I do this to get the full file name.

Next, I need to create the destination folder and copy the xml file to it.

Sponsored

In terms of a quick and easy export or backup that’s all there is to it. But I’m always thinking about what other requirement someone might have. Assuming you might import the configuration file, it might be helpful to provide a new name for the virtual machine. Or remove hard drive references so that if you import on a different Hyper-V host you don’t get ugly errors. Or remove snapshot references. Plus, you most likely want to do this from the comfort of your desktop. So I created a PowerShell function called Export-VMConfiguration.

The command has complete help and examples.

Export-VMConfiguration Help. (Image Credit: Jeff Hicks)

Export-VMConfiguration Help. (Image Credit: Jeff Hicks)

The heart of the command is a series of steps to modify the XML document and save it to the new location. For example, one of the tasks I wanted to accomplish was to add a note to the configuration that reflected this was exported. PowerShell makes it an easy chore to modify XML documents.

Or I can rename and remove drives.

The function uses PowerShell remoting so that you can run the command against a Hyper-V host from your desktop. In fact, your desktop doesn’t need any of the Hyper-V commands because all of those are running on the remote server. This also means that the path you specify, which must already exist, is relative to the remote computer. The export process will create a subfolder under this path for each virtual machine. If you take advantage of the rename process, then the subfolder will reflect the new name.

Now I can export, or backup, just the configuration for select virtual machines on my Hyper-V server.

Here’s the result:

Export-VMConfiguration gives you the ability to export or backup the configuration for select virtual machines on a Hyper-V server. (Image Credit: Jeff Hicks)

Export-VMConfiguration gives you the ability to export or backup the configuration for select virtual machines on a Hyper-V server. (Image Credit: Jeff Hicks)

Sponsored

Or I can get a virtual machine and pipe it to my export command.

This will get a single virtual machine, rename it to CHI-ClientBase, and remove hard drive and snapshot references.

You are welcome to use this function as-is after you test it in a non-production environment. While it works for me, there’s no way I can guarantee it will work for you in your environment. Or you might want to use it as a model or source material for your own Hyper-V and PowerShell projects.

Sponsored

Tagged with , , ,