WMI VBS  – Basics (Windows Management Instrumentation)

Introduction to WMI VBS Basics

Computer scripts are economical with words.  Put it another way, every word counts.  Attention to syntax, is – vital.  By all means merely copy and paste my scripts, however, if you wish to learn about how each WMI command works, please take the time to study the properties and methods of each object.  If you are familiar with Microsoft’s VBScript, (perhaps you have created logon scripts) then skip the basics and head for my examples in the WMI menu to the left.

Topics for WMI – Basics


The Relationship Between VBScript and WMI

My first encounter with VBScript was when I wanted a replacement for batch files to map network drives.  In these new VBScript logon scripts there was not a WMI command in sight.  My point is that VBScript has an existence away from WMI.  Another job for VBScripts is to create or modify Active Directory objects such as Users.  In many ways WMI builds on knowledge gained from logon scripts and creating users, for example, by providing extra command to interrogate the operating system.  Another link is the way WMI uses commands such as winmgmts … root\cimv2, which are equivalent to LDAP:// with dc=domain.

As for pure WMI skills, the best approach is to think of WMI as having its own language with commands that link to scripting languages such VBScript or C++.  In practical terms, look out for scripts with a VBScript beginning, a WMI centre and a VBScript ending.

A Recap of VBScript

The first section of the VBScript, sometimes referred to as the header, states the purpose of the script and declares variables in Dim statements.

I like to start the script proper with Option Explicit because it forces me to declare variables.  My reasoning is this, Option Explicit alerts me to errors, it halts the script if I miss spell a variable, or use the singular colItem whereas I declared the plural colItems in the Dim statement.

When we move into the middle section of the script, WMI prepares the ground by connecting to the root\cimv2.  In the example below, once objWMIService gets a handle on the namespace, we select DiskDrive from Win32_’s many objects.  Moving into the final section, VBScript loops through all the properties that we are interested in, and finally Echoes the results to screen.

Other Pure VBS Commands

My teaching dilemma is making the scripts clear to the beginner, while providing scripts that solve a real world problem. While this section is about WMI, I want to emphasise how VBScript commands enhance WMI scripts.  My approach is to provide a page dedicated to one WMI idea, for example interrogate Event Logs, but add in VBScript techniques such as writing the output to a file.  Each WMI Example has a ‘Learning Points’ section underneath the code, and that section is subsection called : From a VBScript perspective.

VBScript Commands to Watch out for

  • FSO – File System Object to write the output to disk.
  • InputBox – To make you think – which machine should we investigate.
  • Looping – If … then.  End If.  Do …. Loop Until.
  • Select Case – Alternative to multiple If … then.  End If
  • Sub Routine() – For clarity, organize specific sections after the main code.  Particularly useful where you need to repeat code.
  • VBCr and (_) – Syntax may seem trivial, but without these, and other little commands to grease the script, you may find they grind to a halt with nasty error messages.  Waste no opportunity to master the rules of VBScript syntax.
  • UCase, Left, inStr – Even more trivial commands, nevertheless they can be life savers for specialist sections of a script.

I have deliberately used the above VBScript commands sparingly, I did not want them to steal the show from the WMI section of the script.  As a result not all VBScript commands feature in all scripts.  The other side of this coin is that most of my WMI scripts could be modified, for example, to include an InputBox, or section to write the output to file.

Guy Recommends: WMI Monitor and It’s Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Example WMI VBS to Interrogate a Disk

I cannot resist learning by doing, so here is a script to provide WMI examples of what I mean.  The script does a real job of interrogating each Disk Drive in the machine where you run the script.  Incidentally, you do not need Active Directory to run this VBScript.

‘ Disk.vbs
‘ Sample VBS WMI
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 1.5 – November 2010
‘ ———————————————–‘
Option Explicit
Dim objWMIService, objItem, colItems, strComputer, intDrive

‘ On Error Resume Next
strComputer = "."
intDrive = 0

‘ WMI connection to Root CIM
Set objWMIService = GetObject("winmgmts:\\" _
& strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery(_
"Select * from Win32_DiskDrive")

‘ Classic For Next Loop
For Each objItem in colItems
intDrive = intDrive + 1
Wscript.Echo "DiskDrive " & intDrive & vbCr & _
"Caption: " & objItem.Caption & VbCr & _
"Description: " & objItem.Description & VbCr & _
"Manufacturer: " & objItem.Manufacturer & VbCr & _
"Model: " & objItem.Model & VbCr & _
"Name: " & objItem.Name & VbCr & _
"Partitions: " & objItem.Partitions & VbCr & _
"Size: " & objItem.Size & VbCr & _
"Status: " & objItem.Status & VbCr & _
"SystemName: " & objItem.SystemName & VbCr & _
"TotalCylinders: " & objItem.TotalCylinders & VbCr & _
"TotalHeads: " & objItem.TotalHeads & VbCr & _
"TotalSectors: " & objItem.TotalSectors & VbCr & _
"TotalTracks: " & objItem.TotalTracks & VbCr & _
"TracksPerCylinder: " & objItem.TracksPerCylinder

‘ End of Sample Disk VBScript

WMI VBS Tutorial Learning Points

1) strComputer ="."  Here is a classic example of naming a VBScript variable.  In the Hungarian naming convention, str refers to a string variable, other common prefixes are int for integer and obj for object.  It is good practice to name variables after the object to which they refer, in this case the computer.  The ".", dot or period means the current machine, the computer where the script is running.  Therefore, it would be easy to change the "." for the name of a server on your network.  The beauty of using variables is that you only need to set the value once, instead of hunting through the script for every reference of that computer.

2) GetObject("") is a well-known VBScript method for fetching an instance of an object.  Winmgmts is the shell, which looks after the CIM schema and objects.  In Active Directory examples, in place of winmgmts, you may find, LDAP:// or WinNT://.  If it would aid your understanding of this term, please type winmgmt /? at the command prompt.

Root\cimv2, means connect to the beginning of the CIM namespace.  It is shorthand for start at the place where all the WMI objects art stored.  A good analogy would be connecting via the UNC path, \\ server \share.  Another place where you see the root namespace model is in DNS.  Some WMI scripts use \root\default.  Here is a screen shot showing where the link is stored in the registry:


3)The next command is ExecQuery, meaning let us interpret a WQL query.  Perhaps you recognise this WQL command as having a very similar structure to SQL?

Select * from Win32_DiskDrive.  WQL (WMI Query Language) is almost identical to SQL (Structured Query Language) used in databases.  The more you experiment with WMI, the more you will lookout for the Win32 family of processes.  In this case we select DiskDrive, in other examples we will choose ComputerSystem or Printer.

Solarwinds Config GeneratorGuy Recommends: The Free Config Generator

SolarWinds’ Config Generator is a free tool, which puts you in charge of controlling changes to network routers and other SNMP devices.  Boost your network performance by activating network device features you’ve already paid for.

Guy says that for newbies the biggest benefit of this free tool is that it will provide the impetus for you to learn more about configuring the SNMP service with its ‘Traps’ and ‘Communities’. Try Config Generator now – it’s free!

Download your free copy of Config Generator

From a VBScript perspective

4) All script languages include loops constructions.  This VBScript example uses the: For Each..x In y…. Next, construction.  The reason for the loop in this example is that there may be more than one disk.  Pay close attention to the fact that there are not one, but two variables, objItem and colItems, I did consider choosing contrasting names, but Scriptomatic and WBEMTest use these particular name in this context so I reasoned that it would be beneficial to get to know objItem and colItems.

5) I would like to analyse one of the properties in detail, then we can apply the principles to other items.
"Partitions: " & objItem.Partitions & VbCr & _

"Partitions: " is a literal, a label we choose for the item.  To make it more legible, note the space between the colon and the second speech mark.  Incidentally, I often accidentally omit the & (ampersand) and thus trigger an 800xxxx error saying, ‘Expected end of statement’.

objItem.Partitions: each line follows a similar pattern, it is just the property that changes for example, .Partitions .Name .Caption or .Model.  Naturally, a suitable label precedes each item. For example, "Partitions: " & objItem.Partitions.

Be careful to distinguish between the two line break commands _ (underscore) and vbCr.  VbCr, with surrounding ampersands, means give me a line break, in the output.  The only purpose is to make the screen message easier to interpret.

Whereas vbCr is cosmetic and optional, each underscore is essential.  Without this special (_) character, the script would not realize that that the command continues on the next line.  The underlying cause is VBScript does not understand word-wrap.  Therefore, if you omit the underscore, VBScript thinks there are two separate commands.  Without the underscore, neither line makes sense on its own, the result of omitting the underscores is that you get an error.

Summary of Basics of WMI

WMI (Windows Management Instrumentation) provides a wealth of commands so that you can discover all about Microsoft’s operating systems objects, their properties and their values.  What makes WMI difficult at the beginning is that you need to develop many new skills quickly, in particular VBScript and WQL which is like SQL.  No previous knowledge is expected as all will be explained if you follow my examples on the following pages.

If you like this page then please share it with your friends


See more VBScript WMI examples:

WMI Tutorial   • Win32_Process   • WMI Memory   • WMI Basics   • Free Download of WMI Monitor

WMI VBS   • VBScript Services   • WMI Disks   • WMI Physical Disks

WMI Home   • WMI Win32   • WMI Printer   • VBScript Echo   • WMI VBScript