Use PowerShell to Detect if Hypervisor is Present

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to determine if a hypervisor is present

Hey, Scripting Guy! Question Hey, Scripting Guy! I have been trying to use WMI to determine if a server has the hypervisor present. I found the WMI class, and even the properties I need. But alas, the WMI class seems to be broken, and it is causing my Windows PowerShell script to fail. For your information, I have been using the Win32_Processor WMI class because it seems to have the properties I need. I know there are various tools I can download that provide this information, but I prefer to use something native if I can. Can you help me save my script? I have been working on this for days, and if I cannot get this to work, I will look really bad with my boss.

—AZ

Hey, Scripting Guy! Answer Hello AZ,

Microsoft Scripting Guy, Ed Wilson, is here. I can certainly understand how your boss might not be too pleased if after you have spent a couple of days on a script, and you do not have something working to show for the time. Unfortunately, it seems that WMI is often a hit or miss proposition. I mean, for one thing, there is an awful lot of it—like thousands of WMI classes in dozens of WMI namespaces. For another thing, just because I find the perfect WMI class, with the perfect WMI properties, it does not mean that it will actually work for me.

This is especially true when talking about hardware because often hardware must be specifically instrumented to provide certain types of information—and that is often driver dependent. Many times, various hardware makers will include a WMI management (or support) pack that must be specifically downloaded and installed on certain makes and models of equipment. At times, this actually creates additional WMI namespaces related to your specific hardware. This looks like a great opportunity to begin  Troubleshooting Week.

Looking at processor information

The Win32_Processor WMI class (depending on what version of the operating system you have installed) provides good general information about processors. I like to use the Get-Ciminstance cmdlet because I get Tab completion with my WMI classes. Here is the basic command:

Get-CimInstance win32_processor | fl *

I can get the same information by using the Get-WmiObject cmdlet:

Get-WmiObject win32_processor | fl *

I can shorten the commands by using aliases, such as this:

gcim Win32_Processor | fl *

     or

gwmi Win32_Processor | fl *

The output from the basic WMI query is shown here:

Image of command output

The properties that you are probably looking for appear to be:

VirtualizationFirmwareEnabled           : False

VMMonitorModeExtensions                 : False

But because I am running Hyper-V on my laptop, I imagine that this is the WMI class and properties you were complaining about.

Interestingly enough, there is a WMI class that provides Hypervisor information. It is the Win32_ComputerSystem WMI class. This command is shown here:

gcim Win32_ComputerSystem | fl *

As you can see in the following image, there are some properties that do not report back.

Image of command output

But, there is an interesting property, HypervisorPresent, that does appear to report correctly. Here is the command I like to use:

PS C:\> (gcim Win32_ComputerSystem).HypervisorPresent

True

So, AZ, all you need to do is to modify your Windows PowerShell script to use the Win32_ComputerSystem WMI class and to query the HypervisorPresent property.

AZ, that is all there is to using WMI and Windows PowerShell to find out if a hypervisor is present. Troubleshooting Week will continue tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon