WMIC

WMI queries from the command line

With WMIC we can use WMI queries in batch files.

Though the C in WMIC seems to stand for Console, I prefer to interpret it as WMI for the Command line.

To start WMIC in interactive console mode, just type:

WMIC

Typing /? in the WMIC console will give you the same on-screen help you would get after typing:

WMIC /?

at the command prompt: a list of switches and aliases.

Since we are dealing with batch files here, I'll use the commands for Command Line Mode from now on. If you prefer the Interactive Console Mode, just skip the WMIC at the start of each command line.

Now let's try the following commands. Don't worry, GET commands won't do any harm (don't try SET yet):

WMIC BIOS
WMIC BIOS Get Manufacturer
WMIC BIOS Get Manufacturer,Name,Version /Format:csv
WMIC BIOS Get Manufacturer,Name,Version /Format:list
WMIC BIOS Get /Format:list
WMIC BIOS Get Manufacturer,Name,Version /Format:htable

You may want to save the latter to a HTML file to view it in a browser:

WMIC /Output:bios.html BIOS Get Manufacturer,Name,Version /Format:htable
START "" "%CD%.\bios.html"

Need the result pasted in another window?

Use /Output:CLIPBOARD

Need to store the properties in variables? Try this (note the commas now being "escaped" by ˆ, carets):

FOR /F "tokens=*" %%A IN ('WMIC BIOS Get Manufacturerˆ,Nameˆ,Version /Format:list ˆ| FIND "="') DO SET BIOS%%A
SET BIOS

Try to figure out for yourself how that worked.
A hint: try the plain WMIC command, without the FIND filtering and without the FOR loop, and see what the output looks like...
Now prepend each of those lines with SET BIOS

 

A note on aliases

Instead of the BIOS alias, we might just as well have used Path Win32_BIOS, it wouldn't have made any difference.
The reason I mention this is that my WMI Code Generator does indeed use Path followed by the full class name.
Besides, not every class has its own WMIC alias.

To find the full class name behind an alias, use the following command:

WMIC ALIAS alias_name Get Target [ /Format:list ]

The result will look like this:

Target=Select * from full_class_name

or use:

WMIC alias_name Get CreationClassName [ /Format:list ]

The result will be one or more lines like this:

CreationClassName=full_class_name

 

Other computers & other WMI namespaces

Use WMIC's /Node:remote_computer switch to query remote computers, and use the /NameSpace:\\root\other_namespace to query classes that are not located in the default CIMV2 namespace.

 

Work with the registry

It is possible to use WMIC to read, write or delete registry keys and values, but I would not recommend it, it is not for the faint of heart.
REG.EXE is a lot easier to use.

The reason I still mention this feature of WMIC is the possibility to check if the current used has permissions to access a registry key or value:

SET KEY_QUERY_VALUE="&H1"
SET KEY_SET_VALUE="&H2"
SET KEY_CREATE_SUB_KEY="&H4"
SET KEY_ENUMERATE_SUB_KEYS="&H8"
SET KEY_NOTIFY="&H10"
SET KEY_CREATE="&H20"
SET DELETE="&H10000"
SET READ_CONTROL="&H20000"
SET WRITE_DAC="&H40000"
SET WRITE_OWNER="&H80000"

SET HKEY_CLASSES_ROOT="&H80000000"
SET HKEY_CURRENT_USER="&H80000001"
SET HKEY_LOCAL_MACHINE="&H80000002"
SET HKEY_USERS="&H80000003"
SET HKEY_CURRENT_CONFIG="&H80000005"

WMIC /NameSpace:\\root\default Class StdRegProv Call CheckAccess hDefKey=%HKEY_LOCAL_MACHINE% sSubKeyName="Software\My Program" uRequired=%KEY_SET_VALUE%

This code will return "errorlevel" 0 ("true") if the current user has permission to set values in HKEY_LOCAL_MACHINE\Software\My Program or 2 ("false") if not.

To combine permissions, add their associated hexadecimal numbers.

Notes: 1 You don't have to use environment variables to store the hexadecimal values:
WMIC /NameSpace:\\root\default Class StdRegProv Call CheckAccess hDefKey="&H80000002" sSubKeyName="Software\My Program" uRequired="&H2"
will work fine.
  2 The online documentation on MSDN for the CheckAccess method was not entirely accurate.
The uRequired parameter was documented as lRequired, which did not work on my Windows 7 system.
To find the correct parameter name I used WBEMTest (or I could have read the comment in the Community Content at the bottom of the page).

 

Samples

There are some WMIC samples available on this site:

Except for CheckRegAccess.bat, PausePrinting.bat, ResumePrinting.bat, and the reboot and shutdown, these samples are all limited to Get (read) property values only.

More samples (be carefull, some of these do actually change your system configuration):

 

More to explore:

A good source for more information is the article WMIC - Take Command-line Control over WMI by Ethan Wilansky.

Examples of WMIC commands for Windows .NET SERVER Family

Using Windows Management Instrumentation Command-line

Understanding WMI Scripting
Microsoft Windows 2000 Scripting Guide

 

And Alain Lissoir's book: Understanding WMI Scripting which devotes an entire chapter to WMIC.

 

Microsoft Windows 2000 Scripting Guide provides an excellent explanation of WQL use in its chapter "Retrieving Managed Resources Using WMI Query Language".

 

To create more "selective" queries, use the WMI Query Language, or WQL, a subset of SQL.
See this overview of WQL.

QR Code for http://www.robvanderwoude.com/wmic.php

unique visitors since July 2007

page last uploaded: 23 July 2010, 08:58