WMIC

WMI queries from the command line

With WMIC we can use WMI queries in batch files.
Like WMI itself, WMIC is available as of Windows XP Professional.
And though WMI can be added on Window NT 4 and 2000, WMIC requires Windows XP Professional or later.

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 remove "WMIC" from the start of each command line.

WMIC commands

The majority of my batch files uses WMIC's GET command to read properties of the specified WMI class.
To find out which properties are available for a specific WMI class, use a command like:

WMIC OS Get /?

Feel free to experiment with WMIC's GET command, it won't change any setting.

With WMIC's SET command you can set (change) properties, unless they are read-only.
To find out which properties are writeable for a specific WMI class, use a command like:

WMIC OS Set /?

Do not use WMIC's SET command unless you are absolutely sure about the consequences.

With WMIC's CALL command you can call a WMI method for the specified class.
To find out which methods are avalable for a specific WMI class use a command like:

WMIC OS Call /?

Do not use WMIC's CALL command unless you are absolutely sure about the consequences.

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 /Value ˆ| 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 loops, and see what the output looks like...
Now prepend each of those lines with SET BIOS.

Narrowing down the returned selection with WHERE

Often WMI queries return far too much information, so we want to narrow down the selection.
For example, the following query against my own computer returns 27 instances, even though it has only one network adapter:

WMIC Path Win32_NetworkAdapter Get

Note:

If all you want to know is the number of adapters, just list the indexes only:

WMIC Path Win32_NetworkAdapter Get Index

WQL to the rescue!

WQL or WMI Query Language allows us to get only those instances matching the conditions set by a WHERE clause.
The following modified query returns only 2 instances on my computer (it turns out the BlueTooth dongle is also considered a physical network adapter):

WMIC Path Win32_NetworkAdapter Where "PhysicalAdapter=TRUE" Get

This is pretty straightforward.
Now list only adapters manufactured by Realtek.
The following command will not work:

WMIC Path Win32_NetworkAdapter Where "Manufacturer=Realtek" Get

Realtek in this case is a string, so we need to place it in quotes, preferably without confusing them with the quotes for the entire WHERE clause.
The following commands will work:

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 Generatordoes 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:

The first line extracts a list of aliases from WMIC's help screen, and these aliases are "fed" to the second WMIC command line which retrieves the Target class, as discussed above.
Unfortunately, not all WMI classes have an alias, so some (most) will not be included in the list.

The resulting list is not sorted and may contain duplicates.ListWMIClasses.bat extends the code above to display a sorted list without duplicates.

Other computers & other WMI namespaces

Use WMIC's /NameSpace:\\root\other_namespace switch 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 user 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.

Note:

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 just fine.