Is the output format (column order) of wmic deterministic?

Viewed 185

I am executing this in command line:

wmic printer get name,default, drivername

And I need to parse the result in my application.

The result in my PC looks like:

Default  DriverName                           Name                                   
TRUE     Send to Microsoft OneNote 16 Driver  OneNote (Desktop)                      
FALSE    Microsoft Software Printer Driver    OneNote for Windows 10                 
FALSE    Microsoft XPS Document Writer v4     Microsoft XPS Document Writer          
FALSE    Microsoft Print To PDF               Microsoft Print to PDF                 
FALSE    Microsoft Shared Fax Driver          Fax           

My question is: Is the column order going to be the same in all PCs? Is the column order going to be the same in other Windows versions (7, 8, 8.1, 10)?

2 Answers

Regardless of the order of your comma delimited 'get' properies list, the version of Windows, (with a built-in WMIC.exe), or the end user language, the results are always returned in alphabetical order left to right. In your specific case above, that would always be:

  • Column 1: Default, Column 2: DriverName, Column 3: Name.

However, if you are intending to parse those results for your application, please be aware that when the 'table' format is returned those columns are fixed width, so they may not contain the entire property value if its string is longer that the width.

Wherever possible I'd advise that you return those results in a non 'table' format, and preferably one which captures the entire value if you intend to use it in another command. For instance trailing space characters are often included in Manufacturer supplied values. So as the columns are effectively space character delimited, any trailing characters could be omitted when parsing. Even using 'csv' or 'list' formats, any value at the end of a line may still include trailing spaces.

Please also take account that the results returned from WMIC.exe are encoded as UTF-16LE.

This might be easier using PowerShell. Of course, it might take all the fun out of parsing text with cmd. With PowerShell, it does not matter what order the fields appear when the object is rendered to text.

PS C:\> Get-CimInstance -ClassName Win32_Printer | Select-Object -Property Name,Default,DriverName

Name                          Default DriverName
----                          ------- ----------
Webex Document Loader           False Webex Document Loader
Send To OneNote 16              False Send to Microsoft OneNote 16 Driver
Microsoft XPS Document Writer   False Microsoft XPS Document Writer v4
Microsoft Print to PDF           True Microsoft Print To PDF
Fax                             False Microsoft Shared Fax Driver
Related