Why does Start-Process powershell.exe { Read-Host }
work, but Start-Process pwsh.exe { Read-Host }
does not?
I'm aware of the -ArgumentList
switch of Start-Process
, but it makes things more complicated when it comes to escaping quotation marks:
PowerShell 7.0.3 and 7.2.0-preview.3:
Start-Process pwsh.exe -ArgumentList '-Command "& {
''Hello World.''
Read-Host
}"' -Verb RunAs
PowerShell 5.1:
Start-Process powershell.exe {
'Hello World.'
Read-Host
} -Verb RunAs
On the other hand, when creating a new PowerShell session (without Start-Process
), it is still possible to use a script block:
pwsh.exe {
'Hello World.'
Read-Host
}
Am I missing something here or is there a better/different approach which allows a script block to be executed parallel to the rest of a script?
pwsh get a filename by default in arguments.
If you try to run it in CMD you will see this:
CMD> pwsh { 'Hello!' }
The argument '{' is not recognized as the name of a script file. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Usage: pwsh[.exe] [-Login] [[-File] <filePath> [args]]
[-Command { - | <script-block> [-args <arg-array>]
| <string> [<CommandParameters>] } ]
(.. other help from pwsh ..)
It meant if you want to run some command you must write -Command { ... }
in arguments.
But you can write more short command for run pwsh like this:
Start-Process pwsh.exe '-c', {
'Hello World.'
Read-Host
}
-c
is shorter equivalent for -Command