How to open new tab with running specific command in Powershell (Windows Terminal)?

5

What I want is open new tab having Powershell and running specific command (in my case "npm run local").

I was expecting this will work,

wt -w 0 -p "Windows Powershell" npm run local

but, it gives error,

[error 0x80070002 when launching `npm run local']


Is it possible and how?

*Note: I have done lot of research and also read official documentation, but I was not able to find a way.

windows-terminal
asked on Stack Overflow Apr 19, 2021 by Rajan • edited Apr 20, 2021 by Rajan

2 Answers

2

Running Get-Command npm shows that npm is actually npm.cmd, so a CMD interpreted script, not PowerShell. When run directly at the command-line, PowerShell is okay with leaving the extension off, but when passed through a Windows Terminal profile, the extension appears to be required:

wt -w 0 -d . -p "Windows PowerShell" npm.cmd init
wt -w 0 -d . -p "Command Prompt" npm.cmd init

(Edit: -d . added per your comment. Without that, as you pointed out, it will always default to the %userprofile% directory).

That said, it seems from your comment that your use-case is to keep the tab open after the process completes. In that case ...

wt -w 0 -d . -p "Windows PowerShell" cmd /k npm run local
wt -w 0 -d . -p "Command Prompt" cmd /k npm run local
answered on Stack Overflow Apr 19, 2021 by NotTheDr01ds • edited Apr 20, 2021 by NotTheDr01ds
2

I found the best/recommended solution from Windows Terminal developers.

Answer on GitHub

For both Command Prompt and Powershell.

wt --window 0 -p "Windows Powershell" -d . powershell -noExit "npm run local"

Only for Powershell.

wt --window 0 -p "Windows Powershell" -d "$pwd" powershell -noExit "npm run local"


The best use of this feature while running commands for development, in my case

For Powershell

wt --window 0 -p "Windows Powershell" -d . powershell -noExit "npm run startMongo"`;  split-pane --horizontal -p "Windows Powershell" -d . powershell -noExit "npm run gulp-watch"`;  new-tab -p "Windows Powershell" -d . powershell -noExit "npm run local"

For Command Prompt

wt --window 0 -p "Windows Powershell" -d . powershell -noExit "npm run startMongo";  split-pane --horizontal -p "Windows Powershell" -d . powershell -noExit "npm run gulp-watch";  new-tab -p "Windows Powershell" -d . powershell -noExit "npm run local"
answered on Stack Overflow Apr 20, 2021 by Rajan • edited Apr 23, 2021 by Rajan

User contributions licensed under CC BY-SA 3.0