Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the correct way to open powershell core in a new window?

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?

like image 782
Luke Schlather Avatar asked Nov 08 '19 22:11

Luke Schlather


2 Answers

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.

like image 132
mklement0 Avatar answered Sep 25 '22 15:09

mklement0


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.

like image 29
js2010 Avatar answered Sep 26 '22 15:09

js2010