≡ Menu

Find physical host name of a virtual machine using Powershell

The Powershell script discussed in this article will help you to find out the physical host name of a virtual machine using powershell. It works for both VMware based virtual machines and Hyper-V based virtual machines.

Virtualization is growing very fast and many companies switching most of their infrastructure to virtual space. Depending on budget and feature set requirements companies are switching to VMware or Hyper-V virtualization. In some cases, they place critical servers in VMware and low priority in Hyper-V. Since the virtual world is spread across multiple platforms, there is a need to identify the physical host of a given virtual machine. Though companies maintain some sort of naming convention, it is not a full-fledged solution to determine if a host is physical or virtual. This is even truer in cases where P2V is performed to migrate physical machines to virtual world where name of the computer is not changed. I too had such requirement and thought that building a script for this is very useful for everyone.

Each virtual machine stores the physical host information in registry at “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters” in PhysicalHostName value. So to get physical host name of a virtual machine, we should just query the data of aforementioned registry value. The below script helps you in that course. Before querying the registry, first it verifies if the given host is virtual or not by verifying its mode. This is done by querying model attribute of Win32_ComputerSystem WMI class. If it is a virtual machine, then the script makes a remote registry call using [Microsoft.Win32.RegistryKey] dotnet class and connects to HKLM and then queries the value of the key mentioned before.

The script is capable of taking multiple computers from command line find their physical host names.

Save the below code into a file (Get-PhysicalHostName.ps1 for example) and use as shown below.

.\get-physicalhostname.ps1 –computername computer1,computer2

[This code is working for only Hyper-V servers at the moment. I am working on how to device this information for VMware VMs as well]

 [string[]]$ComputerName = $env:ComputerName            
$KeyName = "SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters"            
$ValueName = "PhysicalHostName"            

foreach($Computer in $ComputerName) {            
 if(!(Test-Connection -ComputerName $Computer -Count 1 -quiet)) {             
  Write-Warning "$Computer is offline"            

 try {            
  $CompObj = Get-WMIObject -Class Win32_ComputerSystem -ComputerName $Computer -ErrorAction Stop            
  if($CompObj.Model -notmatch "Virtual") { throw "Not a virtual machine" }            

 } catch {            
  Write-Warning "$Computer : FAILED. Details : $_"            

 try {            
  $BaseKeyObj = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $computer)            
  $KeyObj = $BaseKeyObj.OpenSubKey($KeyName)            
  $Hostname = $KeyObj.GetValue($ValueName)            
 } catch {            
  Write-Warning "$Computer : Failed to get Physical host name. Details : $_"            

 $OutputObj = New-Object PSObject -Prop (            
   ComputerName = $Computer.ToUpper()            
   PhysicalHostName = $Hostname            


Hope this helps…

{ 6 comments… add one }
  • jeevan December 30, 2013, 11:43 am

    Hello Sitaram,
    This script not helped me. I am getting message as “Failed Details : Not a virtual Machine”

    • Sitaram Pamarthi December 30, 2013, 2:38 pm

      You will get that message when run against a physical machine. In such cases there is no question of physical host .

  • jeevan December 30, 2013, 11:43 am

    Hello Sitaram,
    This script not helped me. I am getting message as “Failed Details : Not a virtual Machine”
    i tryed for VMware Virtual machine.

  • Sri December 31, 2013, 10:59 am

    This script will not work for VMware. As mentioned in the above description, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters will not have any info on VMware Virtual machines. As of now you can get physical hostname of VMware VMs only through Powercli.

    • Sitaram Pamarthi December 31, 2013, 11:33 am

      That’s correct Sri. My fault, I spoke without testing. Will make correction in the article.

    • DavidJ September 23, 2016, 5:40 pm

      I agree. The VMware vm’s have no entries under hklm\software\Microsoft\virtualmachine\

Leave a Comment