PowerShell: Run PowerShell from cmd.exe via STDIN
Why Pass Command to PowerShell via STDIN
pass the command to PowerShell via stdin avoid a lot string escape quote problems, such as file name or regex.
Practical Example of a Problem
Sometimes you need to run PowerShell command from
cmd.exe
.
For example, in
Emacs for Microsoft Windows,
when in emacs lisp you call a shell command, it calls
cmd.exe
.
It is difficult to change it to PowerShell.
but you can launch PowerShell from cmd.exe by
pwsh -command args
or
pwsh -command {body}
but if the args has lots quotes or slash or backslash, its error.
and also
{body}
cause error.
Because it tries to parse it.
Calling PowerShell Using Stdin as Script Block
rem calling PowerShell from cmd.exe, passing command body to stdin pwsh -command -
Official PowerShell Documentation
-Command | -c Executes the specified commands (and any parameters) as though they were typed at the PowerShell command prompt, and then exits, unless the NoExit parameter is specified. The value of Command can be "-", a script block, or a string. If the value of Command is "-", the command text is read from standard input. The Command parameter only accepts a script block for execution when it can recognize the value passed to Command as a ScriptBlock type. This is only possible when running pwsh from another PowerShell host. The ScriptBlock type may be contained in an existing variable, returned from an expression, or parsed by the PowerShell host as a literal script block enclosed in curly braces "{}", before being passed to pwsh. pwsh -Command {Get-WinEvent -LogName security} In cmd.exe, there is no such thing as a script block (or ScriptBlock type), so the value passed to Command will always be a string. You can write a script block inside the string, but instead of being executed it will behave exactly as though you typed it at a typical PowerShell prompt, printing the contents of the script block back out to you. A string passed to Command is still executed as PowerShell script, so the script block curly braces are often not required in the first place when running from cmd.exe. To execute an inline script block defined inside a string, the call operator "&" can be used: pwsh -Command "& {Get-WinEvent -LogName security}" If the value of Command is a string, Command must be the last parameter for pwsh, because all arguments following it are interpreted as part of the command to execute. When called from within an existing PowerShell session, the results are returned to the parent shell as deserialized XML objects, not live objects. For other shells, the results are returned as strings.
PowerShell. Profile and Script
- PowerShell: Open File by Default App
- PowerShell: Launch App, Start Process
- PowerShell: Load a Script
- PowerShell: Eval Variable, String, ScriptBlock
- PowerShell: Run PowerShell from cmd.exe via STDIN
- PowerShell: Profile (init file)
- PowerShell: Color Prompt
- PowerShell: Manage PowerShell Profiles
- PowerShell: Create a Script
- PowerShell: File Name Extension
- PowerShell: Get Current Script Path