How can I use a VBScript script to read Windows Management Instrumentation (WMI) information?

John Savill

March 24, 2005

1 Min Read
ITPro Today logo in a gray background | ITPro Today

A. To read WMI information, you must write code that connects to the WMI management object, then runs a query against it. Remember, WMI has multiple classes of information, so you need to query the correct class to retrieve the desired information. These classes are documented in the WMI software development kit (SDK); an online version of the SDK is available here.

The following sample script, which you can download at http://www.windowsitpro.com/content/content/45830/compquery.zip, queries a computer's model and serial number. This information is located in two separate classes, so you need to use two queries.

Dim objWMI : Set objWMI = GetObject("winmgmts:")Dim colSettingsComp : Set colSettings = objWMI.ExecQuery("Select * from Win32_ComputerSystem")Dim colSettingsBios : Set colSettingsBios = objWMI.ExecQuery("Select * from Win32_BIOS")Dim objComputer, strModel, strSerialFor Each objComputer in colSettings  strModel = objComputer.ModelNextFor Each objComputer in colSettingsBios  strSerial = objComputer.SerialNumberNextwscript.echo strModelwscript.echo strSerial

To execute the script from a command line, save the previous script as wmi.vbs, and run this query:

cscript wmi.vbs

You'll see the following on-screen messages:Microsoft (R) Windows Script Host Version 5.6Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.Latitude D8002TVKQ51You can change the code to query any type of information that WMI is aware of.

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like