Search This Blog

Friday, April 20, 2018

Easy to use WMI commands to query windows system

Easy to use WMI commands to query windows system WMI has been around for a very long while and it has grown significantly over the years. As of now though the WMI has been deprecated by Microsoft and the WMI and it's WMIC command line utility is superseded by Windows PowerShell for WMI!

But if you happened to have any legacy, old, outdated servers lying around that you cannot upgraded just yet or maybe ever, 
WMI is maybe the only option you may have available to remotely manage servers from the command line.

And the WMIC is a command line interface to the WMI (Windows Management Instrumentation).

To get started,  launch the command shell (CMD.EXE).

You can run WMIC utility interactively, from the WMIC prompt,  or non-interactively. Once you get little comfortable with it, I think you would use the non interactive method more often.

But for learning purpose, lets start WMIC in interactive mode by typing WMIC on the command prompt and hit enter. it should now display the WMIC command promot,  wmic:root\cli>;


Here on I am just listing some informational commands that you can type in and view the results show up on your display:

Type /? and hit enter.  This would display all available WMIC commands reference.

















To get information on the CPU on local computer


From command prompt: WMIC CPU

OR

In interactive WMIC session: cpu








To run a WMI command on a remote computer


WMIC /NODE:<SERVERNAME> CPU

OR

In interactive session:








To get information on the OS


wmic os

To display only SELECT fields from the output:


wmic diskdrive get name, caption, mediatype, status, size, systemname, totalheads, totaltracks






And here are some examples to get information on the logical disks:

wmic logicaldisk
wmic logicaldisk get caption, deviceid, drivetype, filesystem, status, volumename
wmic logicaldisk where deviceid="c:" get caption, deviceid, drivetype, filesystem, status, volumename


Notice the use of where clause with one the output fields to filter the results.



Get information on a service:


wmic service where name="mssqlserver" get name, caption, startmode, state, status








Wait a minute, what happened? I know I have SQL Server installed on this machine so what the hack?

The reason is simple, all my sql instances are named instances i.e. there is no default instance of SQL on this machine, which would have service name mssqlserver. So, I am going to modify my where condition like this to get all services with the "sql" anywhere in it's path name:

wmic service where "PathName like '%sql%'" get name, caption, startmode, state, status








That's better!

Here are few others that I have found useful at times.

wmic pagefile
wmic pagefileset
wmic memcache
wmic memphysical


And finally, here are some links for additional information:

Using Windows Management Instrumentation Command-line: