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.

0

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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.

6 Comments

Dammit, I can only upvote this once!
Use Get-CimInstance -Class Win32_ComputerSystem instead.
According to 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.
There will always be an equivalent command, if you figure out the true class name from the alias.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.