Tip

Not sure what's running in a virtual machine? Use a batch file

Virtual computing isn’t all that exotic anymore. These days, it’s a big part of how many people do their work. To that end, it helps to know how to deal with scenarios that used to be unusual, but are now common, such as detecting whether or not you’re running a particular program inside a virtual machine.

Why do this? For one, you might not want certain programs running in a virtual environment since they may not perform the way you’d expect. Additionally, it could be a good idea to warn users that they might need to install support for certain kinds of virtual hardware before proceeding.

One quick way to detect whether or not you’re running Windows in a virtual machine is by using a

    Requires Free Membership to View

batch file to pull system information with the WMIC utility. This command-line tool is a front-end for the Windows Management Instrumentation (WMI) subsystem, from which you can harvest a whole slew of low-level information about a given system.

For instance, try this experiment. Fire up a Windows computer that’s running a virtual machine application of some kind -- be it Microsoft Hyper-V, Oracle VM VirtualBox or something from VMware -- and type the following command in both the virtual machine and host OS:

wmic computersystem get model /format:csv

Compare the results. On the host machine, you should get something like this:

Node,Model
<systemname>, Dell XPS420

On the virtual machine, you’ll see something like this:

Node,Model
<systemname>, VirtualBox

Virtual machines tend to report the machine model as the name of the virtualization application, which makes it relatively easy to identify whether or not you’re running a virtual machine without any terribly sophisticated programming.

Joeware.net provides a batch file to do this kind of processing automatically. The script has built-in strings for both VMware virtualization and Hyper-V. It’s simple enough that you can easily add other virtualization technologies like VirtualBox, as long as you know what strings they return for the machine model.

If you’re not using conventional batch files anymore, the same data can be retrieved in Windows PowerShell using the Get-WmiObject command (also abbreviated as gwmi):

Get-WmiObject Win32_Computersystem model

For more concise output you can use:

Get-WmiObject Win32_Computersystem | Format-List model

The manufacturer attribute can also be used in conjunction with model to determine if you’re in a virtual machine. VirtualBox, for instance, returns a manufacturer of “innotek GmbH” (the original creators of VirtualBox). Such details can easily be harvested from various virtual machines and checked against by whatever script you’re using.

You can follow SearchWindowsServer.com on Twitter @WindowsTT.

ABOUT THE AUTHOR
Serdar Yegulalp has been writing about computers and information technology for more than 15 years for a variety of publications, including InformationWeek and Windows Magazine.

This was first published in November 2010

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.