Once you start to get more and more virtual machines, and more and more Hyper-V servers, in your environment it can get quite hard to keep track of where a specific virtual machine is actually running.
System Center Virtual Machine Manager can help you out here – but what if you are not sitting at the System Center Virtual Machine Manager Console? What if you have used Remote Desktop to connect to the virtual machine? What do you do then?
A long, long time ago – I posted information on how to figure out the host operating system from inside a virtual machine using a VBScript on Virtual PC and Virtual Server. And this script also works on Hyper-V!
But no one uses VBScript anymore! Right? So how do we do this in PowerShell?
The answer is with a one-liner of course! (or actually – three one liners).
To get the name of the physical computer that you are running on, open a PowerShell inside the virtual machine and type in:
(Get-ItemProperty –path “HKLM:SOFTWAREMicrosoftVirtual MachineGuestParameters”).PhysicalHostName
You can also get the fully qualified name of the physical computer by running:
(Get-ItemProperty –path “HKLM:SOFTWAREMicrosoftVirtual MachineGuestParameters”).PhysicalHostNameFullyQualified
Finally, you can get the name of the virtual machine itself by running:
(Get-ItemProperty –path “HKLM:SOFTWAREMicrosoftVirtual MachineGuestParameters”).VirtualMachineName
And of course – this will work on Hyper-V, Virtual Server and Virtual PC.