Skip to main content
Scripting in WMI

You can view or manipulate any information made available through WMI using scripts that access the Scripting API for WMI. Scripts can be written in any scripting language that supports Microsoft ActiveX script hosting, including Visual Basic Scripting Edition (VBScript), PowerShell, and Perl. Windows Script Host (WSH), Active Server Pages, and Internet Explorer can all host WMI scripts.

The following VBScript code example illustrates a number of WMI scripting features. This script enumerates all of the disks on the local computer system.


On Error Resume Next
For Each Disk In GetObject( _
    "winmgmts:").InstancesOf ("CIM_LogicalDisk")
        WScript.Echo "Instance:", Disk.Path_.Relpath
Next
If Err <> 0 Then
set lasterr = CreateObject("WbemScripting.SWbemLastError")
    Wscript.echo lasterr.Operation
End If

If this script is saved in a text file and run, it is executed by WSH. A window is displayed for each disk object on the local computer (for example, "Instance: Win32_LogicalDisk.DeviceID="A:"").

The following example is a compact line of code that accomplishes several tasks.


For Each Disk In GetObject( _
    "winmgmts:").InstancesOf ("CIM_LogicalDisk")

In the previous code example, the following tasks are accomplished:

The following example illustrates several important features of WMI scripting.


WScript.Echo "Instance:", Disk.Path_.Relpath

The following features are illustrated in the previous code example:

  • Path_ is a property of the special generic object SWbemObject.

    Properties or methods ending in an underscore usually belong to this extended object, SWbemObjectEx. In the script line above, The Disk variable refers to a Win32_LogicalDisk instance, which does not have a Path method (or equivalent). Because any WMI object can use the methods of SWbemObject, Disk.Path_ can be called to return an SWbemObjectPath object containing the WMI path for the disk object. The generic object, SWbemObject, can represent any WMI object.

  • WMI paths can locate a particular object in a namespace.

    The SWbemObjectPath.Relpath property is displayed by WSH; for example, ""Win32LogicalDisk.DeviceID="D""".

  • Properties with the Key qualifier are the final part of the path to locate a particular instance.

    The DeviceID property is the key property for the Win32_LogicalDisk class. For more information, see Managed Object Format (MOF).

  • Direct access and chaining objects, methods, and properties.

    You can directly access the properties and methods of WMI objects as shown in Disk.Path_.Relpath. For more information, see Manipulating Class and Instance Information.

    Note  When querying for property values with a uint64 or sint64 data type in a scripting language like VBScript, WMI returns string values. Unexpected results can occur when comparing these values, because comparing strings returns different results than comparing numbers. For example, "10000000000" is less than "9" when comparing strings, and 9 is less than 10000000000 when comparing numbers. To avoid confusion you should use the CDbl method in VBScript when properties of type uint64 or sint64 are retrieved from WMI.
     

The following code example demonstrates that error handling in WMI scripts goes beyond the capabilities of the WSH Err object. By creating an SWbemLastError object, you can get WMI-specific information on the error that occurred. Note that the SWbemLastError object is created by the standard VBScript CreateObject method with the ProgID value of "WbemScripting.SWbemLastError". This is another method of obtaining a WMI object.


If Err <> 0 Then
set lasterr = CreateObject("WbemScripting.SWbemLastError")
    Wscript.Echo lasterr.Operation
End If

For more information about books, websites, and articles on writing WMI scripts and using WMI, see Further Information.

Related topics

WMI Tasks for Scripts and Applications
Scripting API for WMI
Creating a WMI Application or Script