Get the name of a VM in Hyper-V from inside the VM

Something I discovered today in working on a project – it is possible to find out what the VM is named in Hyper-V from within the guest OS.  This is only possible if the IC’s are installed.  It is a registry key and can be queried in a script.

Example:

$Name = Get-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters" -Name VirtualMachineName

$Name.VirtualMachineName

This may be different than issuing “hostname” at the command line.  That would tell you the OS name for the guest machine.  The registry key above is specifically looking for the name of the VM in Hyper-V that represents the machine you are on.