Recovering Virtual Machines in Hyper-V Server 2012 R2: Part 2

Summary: Use Hyper-V cmdlets to identify missing network configurations in virtual machines.

Hey, Scripting Guy! Question Hey, Scripting Guy! This week we are working on some business continuity and disaster recovery practices. One of the documents we are building involves how to rebuild a Hyper-V virtual machine in our Windows Server 2012 R2 environment. Can you tell me if there is a way to identify the configuration data for Hyper-V, specifically the network names in our virtual switches?


Hey, Scripting Guy! Answer Hello BR,

Honorary Scripting Guy, Sean Kearney here—filling in for our good friend Ed. He’s still digging around with some old scripts he found in the back room.

Note  This is the second post in a series. If you missed Part 1, see Recovering Virtual Machines in Hyper-V Server 2012 R2: Part 1.

Hyper-V Server 2012 and Hyper-V Server 2012 R2 include possibly the coolest cmdlet ever. It’s called Compare-VM. Compare-VM has a simple task. Look at a virtual machine’s configuration and see what’s missing from the Hyper-V box.

We used to sing a song like that as a kid when I was in school, “Which of these things is not like the other?” Well, that’s what Compare-VM does.

So let’s pretend that we spun up a brand new Hyper-V Server 2012 R2 box. We have copied the .xml files, VHDs, and all the configuration files from a previous Hyper-V virtual machine.

In Windows Server 2008 R2, if you did not export it in advance, you could get away with re-creating the virtual machine and re-attaching the VHD file. But if you didn’t have the network settings, the virtual machine would have to detect the network card, which meant the usual game of:

  • Hope you have cached credentials.
  • Cross your fingers that it wasn’t a domain controller.
  • Wait…wait…wait…

But with Compare-VM, I can point to the file and obtain what’s missing on my Hyper-V box.

So let’s look at a sample virtual machine. I literally used a Robocopy to bring it to my brand new Hyper-V 2012 R2 Server box.

I’ve just run Compare-VM against an .xml file, and I get this output:

Image of command output

Notice the Incompatibilities property? Let’s access that property to see what it has to say for us. We see a message that reveals why the machine couldn’t connect:

Image of command output

If we expand the message, we’ll find a more revealing answer:

Image of command output

This means that the reason I can’t import this virtual machine and “make it work” on a new box is as simple as “the virtual network switch with the explicit name PublicNetwork does not exist.”

But rather than pulling down 150 individual virtual machines, individually accessing their configurations, and trying to pull the network names, we can simply script it.

We’re going to leverage the .csv file for this script. So ideally, we could have virtual machines that we recovered from a back-up or that we switched to a lab, and then pull out the missing network data:

$HyperVList=IMPORT-CSV C:\HyperVList.csv

$ImportErrors=Foreach ($Machine in $HyperVList)


            $VMError=(Compare-VM –path $Machine.XMLFile).InCompatibilities.Message



$ImportErrors | Select-object Error –Unique

What this should give us is a nice simple list of missing network switches in our virtual machines. With this, we can re-create them by using Windows PowerShell.

But that’s a story for tomorrow…

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Sean Kearney, Honorary Scripting Guy and Windows PowerShell MVP

Comments (10)

  1. Anonymous says:

    The most easiest way to recover data from Virtual machine with the help of this application.

  2. Anonymous says:

    VHD is a file format of virtual machine. I also have a .vhd file but due to some reasons the data of my VHD got corrupted and I needed to fix it.Unfortunately I also didn’t have a fresh backup of VHD. Then I found a superb Tool named VHD data recovery
    tool Unfortunately I also didn’t have a fresh backup of VHD. After using its free demo I actually got all my data back along with deleted VHD file items. I followed this URL & get the excellent application.

    Read More:

  3. Anonymous says:

    The most easiest way to recover data from Virtual machine with the help of this application.

  4. Anonymous says:

    VHD Software is an advance and professional application and this tool is able to images, documents ,attachments, zipped etc from corrupt VHD. It is effective and easy to use application.For more information and free download click

  5. jrv says:


    Good post.  Useful.

    Note that Compare-VM is also in Windows 8.

  6. vijay says:

    Great post!!

    Please could you tell us about how to recover disks?

    After restarting Cluster services I found my CSV3 data moved to CSV5 so VM not booting as It cannot find the disk.

    Any idea or script?



  7. SuperDale says:

    I'm getting a error on $ImportErrors=Foreach ($Machine in $HyperVList) …it says "unexpected token 'in' in expression or statement.

  8. alexsavilas says:

    Get Virtual Machine Data Recovery is an ultimate utility to get back deleted or lost data from Microsoft (VHD), Oracle (VDI) and VMware (VMDK) image files. It can performs recovery from virtual drives based on exFAT, NTFS, FAT16, and FAT32 file systems.It
    is supported by Windows 8,7, Windows Server 2008 R2, 2012 R2 and Hyper-V Server. To know more:

  9. CarlHayden says:

    VHD provides effective and quick solution to recover data from corrupt VHD, it provides free tool for trial version to recover lost VHD files.
    Go here :-

  10. Ivan says:

    I used a Robocopy to bring it to my brand new Hyper-V 2012 R2 Server box.
    But I can’t import this virtual machine. Thanks Scripting Guy

Skip to main content