Search code examples
jsonpowershellparsingwmiwmic

How to parse the output of the Windows command `wmic` using PowerShell


I am trying to parse the output of:

wmic copmutersystem

and

net.exe config workstation

using PowerShell to get an object as Key/Value pair and/or convert it to JSON String.

I know there is a PowerShell equivalent command:

Get-CimInstance  -Class Win32_computersystem

But the ask here is to figure out how to use PowerShell to parse a similar output for wmic CMD line.


Solution

  • Wmic can output csv or xml, but obviously get-wmiobject or get-ciminstance is preferred. You just need to find the class names instead of the aliases. The creator of wmic and powershell is the same.

    wmic computersystem list /format:csv | convertfrom-csv | select model
    
    Model
    -----
    OptiPlex 7490 AIO
    

    List wmic class aliases:

    wmic alias list brief
    
    wmic alias where "friendlyname = 'computersystem'" list brief
    wmic alias where "friendlyname like '%comp%'" list brief
    
    FriendlyName    PWhere  Target
    ComputerSystem          Select * from Win32_ComputerSystem
    

    For example:

    ComputerSystem                                   Select * from Win32_ComputerSystem
    
    get-ciminstance win32_computersystem