5

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?

Luke Schlather
  • 367
  • 2
  • 13

2 Answers2

7

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
1

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.

js2010
  • 23,033
  • 6
  • 64
  • 66