Skip navigation

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

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, strSerial
For Each objComputer in colSettings
  strModel = objComputer.Model
Next
For Each objComputer in colSettingsBios
  strSerial = objComputer.SerialNumber
Next
wscript.echo strModel
wscript.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.6 Copyright (C) Microsoft Corporation 1996-2001. All rights reserved. Latitude D800 2TVKQ51 You can change the code to query any type of information that WMI is aware of.
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish