Q: From the Hyper-V host, how can I get key value pair (KVP) information from a virtual machine (VM)?

A: The KVP functionality of Hyper-V enables data exchange between the guest OS in the VM and the Hyper-V host through the reading and writing of specific registry values. Guests can get information about the host they're running on by viewing values in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters. For example, to get the host name of the Hyper-V host from within a VM I can use:

$regPath = &quot;HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters&quot;<br />
        (Get-ItemProperty -Path $regPath).HostName

It's a little more complex to get values from the Hyper-V host about a specific VM. WMI must be used. In the example below, I fetch the fully qualified domain name of the savdaldc02 VM. (The full list of available values can be found at Microsoft's website.

$vmName = "savdaldc02"

$vm = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem `
    -Filter "ElementName='$vmName'"

$vm.GetRelated("Msvm_KvpExchangeComponent").GuestIntrinsicExchangeItems | % {
    $GuestExchangeItemXml = ([XML]$_).SelectSingleNode(`
        "/INSTANCE/PROPERTY[@NAME='Name']/VALUE[child::text()='FullyQualifiedDomainName']")

    if ($GuestExchangeItemXml -ne $null)
    {
        $GuestExchangeItemXml.SelectSingleNode( `
            "/INSTANCE/PROPERTY[@NAME='Data']/VALUE/child::text()").Value
    }
}