wmic with subcommand in sequence with powershell. (" wmic" then next line "csproduct", I Have pasted in powershell. the csproduct not pasted in powershell as subcommand.
2 Answers
You're pasting multiple lines, which are executed individually:
First,
wmicalone is executed, which instantly causes it to enter thewmicREPL, where you can submit commands interactively.It is only afterwards that PowerShell - which isn't in control while the
wmicREPL is active - tries to paste the other line,csproduct; that is, you'll see this word pasted after exiting the REPL, typically withexit.
To simply execute the csproduct sub-command, place both wmic and csproduct on the same line and paste that: wmic csproduct
This will make wmic execute the sub-command, return its output and exit right after, returning control to PowerShell.
If your intent is to supply multiple subcommands, via strings or a file:
- Via strings:
'csproduct', 'context' | wmic
- Via a file:
'csproduct', 'context' > subcmds.txt
Get-Content subcmds.txt | wmic
However, note that this prints the REPL prompt before each subcommand, as well as one final time, so you may want to perform separate wmic calls, with the help of ForEach-Object:
Get-Content subcmds.txt | ForEach-Object { wmic $_ }
Taking a step back:
- As js2010's helpful answer points out, from PowerShell it is preferable to access WMI via the CIM cmdlets, such as
Get-CimInstance, as it returns objects rather than text; technically,wmicis deprecated - see this answer.
- 312,089
- 56
- 508
- 622
-
@MiteshPatel, pleas see my update. If that doesn't help, please clarify your use case. – mklement0 Aug 17 '21 at 18:42
Or this is the same thing in powershell, csproduct being a wmic class alias for win32_computersystemproduct, and you'll get an object output instead of text.
get-ciminstance win32_computersystemproduct
IdentifyingNumber : ABCDEFG
Name : 4321AB5
Vendor : LENOVO
Version : ThinkCentre M92z
Caption : Computer System Product
- 17,785
- 4
- 45
- 50