Techibee.com

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.

Exit mobile version