Finding the Hyper-V VM Configuration File.

August 4, 2014 by Christopher Walker

Today I found that in my haste to set up my Hyper-V virtual machines, I didn’t properly account for the XML files it creates to use as configuration files for the VMs. I had haphazardly chosen random places accidentally, not realizing the full extent of what I was doing. Chalk it up to inexperience. I went searching for ways to find my haphazardly placed VM config files. I wasn’t getting to far so I figured I’d open up PowerShell and see what I could do with the Get-VM CmdLet provided in the Hyper-V module.

Lo and behold, what do you know, a quick Get-VM Server1 | Get-Member spit out the ConfigurationLocation member of the VirtualMachineBase type. So the following code gets me my VM config files:

  
  
$VM = Get-VM Server1
$Config = $VM.ConfigurationLocation
 
#If you just need the config location and don't need to hold onto the VM
$Config = (Get-VM Server1).ConfigurationLocation
  

Having the configuration location is all well and good, but since I’ve wasted the time to open an admin elevated PowerShell session, I might as well put it to good use. Originally I had just wanted to know where the files were located, but I figured if a location of a config was in a place I didn’t want it to be, I could just move it right then with a little PS CmdLet magic and file management.

A few things you should know before continuing:

  • Moving a VM config requires it to be shutdown completely. Save/prep/plan properly before doing it
  • The way I’m going to do it will just move the config file, not the associated storage
  • If you are unsure before you pull the trigger on script execution, just run a -WhatIf on any of the commands
  
  
$Moving = @() #An array to hold VMs that will be moved

#Begin the magic
Get-VM | ForEach-Object {
    If ($_.ConfigurationLocation -ne $DesiredDirectory) {
        $Job = Stop-VM -VM $_ -AsJob #I put a confirm, you can remove if you want
        Wait-Job $Job
  
        $Moving += $_
    }
}
Stop-Service VMMS
  

Now that everything is stopped, you can pick everything up and move it. You can either do this with PS or do it manually, but put simply, you would take the GUID-named XML files and move them to your desired directory. I chose to move mine back to the default location in C:\ProgramData\Microsoft\Windows\Hyper-V\Virtual Machines to prevent having to make symlinks but if you choose a different directory you will have to recreate the symlinks that exist in the default directory. Using mklink will help with that where the syntax is:

mklink C:\ProgramData\Microsoft\Windows\Hyper-V\Virtual Machines\<symlink XML reference file> <actual XML file location>

Once you have moved everything, you can run the following to take everything back on line.

  
  
Start-Service VMMS
ForEach ($VM in $Moving) {
    Start-VM $VM
}
  

Privacy Policy

© 2017 | Powered by Hugo and lots of motivation.