This command opens a new powershell window, runs the commands, and then exits:
Start-Process powershell { echo "hello"; sleep 1; echo "two"; sleep 1; echo "goodbye" }
If I instead launch Powershell Core, it opens a new window but the new window exits immediately:
Start-Process pwsh { echo "hello"; sleep 1; echo "two"; sleep 1; echo "goodbye" }
What's the correct way to do this sort of invocation with pwsh?
Don't use a script block ({ ... }
) with Start-Process
- it binds as a string to the -ArgumentList
parameter, which means its literal contents - except for the enclosing {
and }
- is passed.
In Windows PowerShell (powershell.exe
), the CLI's default parameter is -Command
.
In PowerShell Core (v6+, pwsh.exe
/ pwsh
), it is -File
[1], which is why your command fails.
In PowerShell Core, you must therefore use -Command
(-c
) explicitly:
Start-Process pwsh '-c', 'echo "hello"; sleep 1; echo "two"; sleep 1; echo "goodbye"'
[1] This change was necessary in order to properly support use of PowerShell Core in shebang lines on Unix-like platforms.
Also, if you ran start-process -nonewwindow
, you would see the error message "cannot find the file". And running a script like this should work: start-process pwsh .\script.ps1
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With