Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What I want is to open a new tab having Powershell and running a 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 an error,

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


Is it possible and how?

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

like image 609
Rajan Avatar asked Apr 19 '21 17:04

Rajan


2 Answers

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"
like image 126
Rajan Avatar answered Sep 22 '22 18:09

Rajan


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
like image 33
NotTheDr01ds Avatar answered Sep 20 '22 18:09

NotTheDr01ds