Command Line WMI: Formatting Data

Posted on April 26, 2012 by Jeff Hicks in Windows Server with 0 Comments

In the last few articles, we’ve been learning how to take advantage of Windows Management Instrumentation, or WMI. Specifically, in part one we we looked at basic syntax and querying for the local computer, discovering ways to gather WMI information using the command line tool WMIC. In part two, we learned how to query remote machines and work with WMIC right from the command line.

Now, in part three, we’ll take a closer look at some advanced ways of formatting data.

If you’ve been following along, you’ve seen me use expressions like this to format the output:

 

Otherwise, WMIC would have displayed everything as one long line. But where did I discover /format? This option is a switch parameter for GET and LIST. Run a command like this, using any alias:

You’ll see a list of switch parameters. Each of these has its own help.

Sponsored

You’ve already seen me use a list and table as the default. What about creating a CSV file?

Actually, this didn’t create the file, only formatted the output. But not a problem based on what I covered in the last article.

Unfortunately, this ends up inserting a blank line at the beginning of the file, which could be problematic depending on how you need to use it. But it takes not much more effort to save all information from a list of computers to a CSV file.

How about creating an XML document? Looking at help, you’ll see two XML related options. I would suggest a command like this:

Or how about creating a quick and dirty HTML report?

This will create an HTML page like Figure 1:

A WMIC Generated HTML Form

Figure 1 A WMIC Generated HTML Form
Or perhaps you’d prefer a table.

A WMIC Generated HTML Table

Figure 2 A WMIC Generated HTML Table
All of these formats are controlled by XSL files you’ll find under C:\Windows\System32\WBEM, most likely in a localized subfolder like EN-US. I’ve never tried creating my own files, but you are welcome to modify these, after making a suitable backup of course.

I’ll wrap this up by creating a single HTML report that contains WMI information from 3 different classes. Because I’m in the interactive mode, the context will retain values for Output and and Append so I only need to enter them once.

Running this from the command prompt, I would be a bit more explicit and specify output and append parameters. In fact, here’s a batch file that demonstrates how you might want to use WMIC.

Sponsored

The batch file takes a computer name as a parameter but defaults to the local host. It creates an HTML report with the 3 WMI classes.

Overview

In some respects, using WMIC to create these types of reports is even easier than using PowerShell. The bottom line is, don’t think you need scripting or even PowerShell to work with WMI. This command line tool might appear a bit daunting at first, but with patience and some trial and error you’ll quickly discover many ways to use it.

Sponsored