1

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
tarekahf
  • 738
  • 1
  • 16
  • 42

2 Answers2

5

Use the Get-CimInstance and ConvertTo-Json commandlets:

Get-CimInstance -Class Win32_ComputerSystem | ConvertTo-Json

Edit: Previous revision of this answer used Get-WMIObject, but that's been deprecated.

zdan
  • 28,667
  • 7
  • 60
  • 71
  • Dammit, I can only upvote this once! – Jeff Zeitlin Apr 27 '22 at 17:21
  • 2
    Use `Get-CimInstance -Class Win32_ComputerSystem` instead. – codaamok Apr 27 '22 at 17:33
  • According to https://github.com/PowerShell/PowerShell/issues/4766, I have to use this command instead `Get-CimInstance -Class Win32_computersystem`. Thank you. However, my intension of the question is how to parse a similar output if there is no equivalent command in PowerShell. I will update the question. – tarekahf Apr 27 '22 at 17:36
  • There will always be an equivalent command, if you figure out the true class name from the alias. – js2010 Apr 27 '22 at 17:38
  • To elaborate on why `Get-CimInstance` is preferable: The CIM cmdlets (e.g., `Get-CimInstance`) superseded the WMI cmdlets (e.g., `Get-WmiObject`) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell (Core) v6+, where all future effort will go, doesn't even _have_ them anymore. Note that WMI still _underlies_ the CIM cmdlets, however. For more information, see [this answer](https://stackoverflow.com/a/54508009/45375). – mklement0 Apr 27 '22 at 17:43
  • What is the equivalent for `net.exe config workstation`? – tarekahf Apr 27 '22 at 17:45
5

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
js2010
  • 23,033
  • 6
  • 64
  • 66