Freigeben über


Finding a Virtual Machine Configuration File

Oh, how I do like a PowerShell challenge.

The other day a colleague asked me this – “Ben, do you have a PowerShell snippet that I could give to a customer that tells them which virtual machine configuration file belongs to which virtual machine?”

Now, if you have ever looked at your virtual machine configuration files – you have likely seen a bunch of weird looking filenames.  And it is not at all obvious which file belongs to which virtual machine. 

So what do you do?

You could run “get-vm | select name, ID” – because most of the time the virtual machine configuration file is the virtual machine ID.  However, hopefully you noticed that I said “most of the time”.  There are definitely times when the configuration file name no longer matches the virtual machine ID.

Fortunately, this information is available.  Unfortunately, it is not exposed directly in our PowerShell cmdlets.  But – you can get it with one of my unusually long PowerShell one-liners.  Like this:

gwmi msvm_virtualsystemsettingdata -namespace root\virtualization\v2 -Filter "VirtualSystemType='Microsoft:Hyper-V:System:Realized'" | %{ write-host "Virtual machine name: $($_.ElementName)"; write-host "Virtual machine ID: $($_.VirtualSystemIdentifier)"; write-host "Configuration file: $($_.ConfigurationDataRoot)\$($_.ConfigurationFile)"; write-host}

Running this on my laptop gives me this:

image

The name, ID and configuration file path for each of my virtual machines.

Cheers,
Ben