≡ Menu

Get list of virtual machines on Hyper-V using Powershell and WMI

Querying Hyper-V made simple with Windows 8.1 and Windows Server 2012. But these two are latest version of Operating Systems and most of the people haven’t tried them yet in corporate environment. In this post I will demonstrate how to use WMI and Powershell to query a Hyper-V server without the use of MS given Powershell module or any other third party module.

When Hyper-V role is installed on a Windows server, it creates a set of WMI classes which we can query to get the required information.

To see list of WMI classes available, just run the below command on a Server where hyper-V role is available. All the Hyper-V related WMI classes are under “root\Virtualization” name space. You can query these classes remotely also like any other WMI class.

Get-WmiObject -Namespace root\virtualization -List

The WMI classes count might vary from Windows 2008 to Windows 2012 because of added features set. In this demonstration let us see how to get list of virtual machine names from Hyper-V using these WMI classes.

Details of virtual machines are stored under Msvm_ComputerSystem WMI class so querying this should list the names of the VMs hosted on the Hyper-V server.

Get-WmiObject -Class Msvm_ComputerSystem -Namespace "root\virtualization" | ? {$_.Caption -eq "Virtual Machine" } | select ElementName

If you notice the above command, I have used additional filtering by caption because by default this WMI class returns the name of the Hyper-V host as well – so to remove it from the list, I used filter to list down only virtual machines.

Hope this helps and happy learning.

Comments on this entry are closed.

  • test August 15, 2016, 9:50 pm

    Nice Tut!

  • PRAVIN PAWAR January 10, 2017, 1:34 pm

    Hi,

    We would like to know the details of all the guest machines on the hyper-v host/ESX.
    e.g. the guest host, memory , CPU etc using WMI query on the host machine.

    What is the way to do achieve this ?

  • Sanderkl October 9, 2017, 11:46 am

    In windows 2016, MS changed the namespace from root\virtualization to root\virtualization\v2.

    So this instead use:
    Get-WmiObject -Class Msvm_ComputerSystem -Namespace “root\virtualization\v2” | ? {$_.Caption -eq “Virtual Machine” } | select ElementName

  • Nick October 11, 2019, 6:04 pm

    I can get the VM list by doing Get-VM also. Can you please highlight the difference between getting the VMs using gwmi vs. getting the VMs using Get-VM cmdlet?

    • Wintel Rocks October 12, 2019, 3:46 pm

      Nick, Get-VM is always easy and preferred. However, it requires the Hyper-V module to be installed. The WMI approach is useful in cases where there is no Hyper-V module installed/available.