PowerShell: Get Hyper-V Host Name from Inside Guest VM

$guestVMName="SOMENAME"

function getHyperVHostname{
param([string]$guestVMName=$env:computername)
$hive = [Microsoft.Win32.RegistryHive]::LocalMachine;
$keyPath = 'SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters';
$value = 'HostName';
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($hive, $guestVMName);
$key = $reg.OpenSubKey($keyPath);
return $key.GetValue($value) ;
}

getHyperVHostname -guestVmName $guestVmName

Leave a Reply

Your email address will not be published. Required fields are marked *