I have a powershell script that opens several powershell terminals and cd's into different directories and runs the applications in those directories:
start powershell @'
$host.ui.RawUI.WindowTitle = \"application 1\"
cd application1\backend
npm install
npm run build
npm run start
'@
start powershell @'
$host.ui.RawUI.WindowTitle = \"application 2\"
cd application2\backend
npm install
npm run build
npm run start
'@
start powershell @'
$host.ui.RawUI.WindowTitle = \"application 3\"
cd application3\backend
npm install
npm run build
npm run start
'@
As you can see, the first thing I do for each terminal is set the title. However, the title gets replaced with each subsequent command. For example, for application1, I see "application 1" as the title but then "npm install" and then "npm run build" and then "npm run start".
How do I set the title so that it sticks? That is, I don't want the title changing to each subsequent command after setting it.
Note... I tried this:
start powershell @'
cd application1\backend
npm install
npm run build
npm run start
$host.ui.RawUI.WindowTitle = \"application 1\"
'@
...hoping the title would stick since it would be the last command to run, but the terminal stays on npm run start
because the application doesn't quit until I hit ctrl-c.
Is there another way to accomplish what I'm trying to do?
Thanks!
I have a powershell script that opens several powershell terminals and cd's into different directories and runs the applications in those directories:
start powershell @'
$host.ui.RawUI.WindowTitle = \"application 1\"
cd application1\backend
npm install
npm run build
npm run start
'@
start powershell @'
$host.ui.RawUI.WindowTitle = \"application 2\"
cd application2\backend
npm install
npm run build
npm run start
'@
start powershell @'
$host.ui.RawUI.WindowTitle = \"application 3\"
cd application3\backend
npm install
npm run build
npm run start
'@
As you can see, the first thing I do for each terminal is set the title. However, the title gets replaced with each subsequent command. For example, for application1, I see "application 1" as the title but then "npm install" and then "npm run build" and then "npm run start".
How do I set the title so that it sticks? That is, I don't want the title changing to each subsequent command after setting it.
Note... I tried this:
start powershell @'
cd application1\backend
npm install
npm run build
npm run start
$host.ui.RawUI.WindowTitle = \"application 1\"
'@
...hoping the title would stick since it would be the last command to run, but the terminal stays on npm run start
because the application doesn't quit until I hit ctrl-c.
Is there another way to accomplish what I'm trying to do?
Thanks!
Share Improve this question asked Nov 20, 2024 at 16:34 Gibran ShahGibran Shah 1,1094 gold badges17 silver badges35 bronze badges 5 |2 Answers
Reset to default 1The trick is that I have to set the title in the application, not from the command line or the script. I had to add the line process.title = 'application 1';
to the application code.
To provide some background and offer an alternative to your own solution:
npm
temporarily changes the terminal title while it runs to its invocation command line, e.g.,npm run test
; that is, it automatically reverts to the previous title when it exits.Short of configuring your terminal to disallow title changes altogether (see this answer for how do to that in Windows Terminal), this cannot be avoided, but you can override the title from within the code being invoked by
npm run
:If what is being invoked is (ultimately) Node.js JavaScript code that is under your control, you can set
process.title
to the title of interest, as shown in your own answer.Otherwise or if you'd rather not modify your application, your only option is to modify the
"scripts"
keys in yourpackage.json
file to prepend a shell-appropriate title-change command to the existing shell command lines - see below.
Sample "start"
property in the "scripts"
property of a package.json
file, building on the approach in this answer:
In this example, the shell-specific title-changing code, which sets the title to
Custom Title
, is followed by printing a message telling the user that the title was set, and waiting for the user to press Enter.
In the real world, the title-changing code would be followed by the original command line for the"scripts"
entry at hand.After inserting the shell-appropriate
"start"
line from below into a given project'spackage.json
, invokingnpm run start
from that project's directory should exhibit the behavior described.- The custom terminal title should display until the user presses Enter.
Using cmd.exe
as the shell (which is npm
's default on Windows):
"scripts": {
"start": "title Custom^ Title & echo Custom terminal title is now set, will revert on exit. & pause"
// ...
},
Using powershell.exe
; this assumes you've configured it as npm
's shell, via npm config set script-shell powershell
; or, if you've installed PowerShell (Core) 7, via npm config set script-shell pwsh
, which also works in Unix-like environments:
"scripts": {
"start": "[Console]::Title='Custom Title'; 'Custom terminal title is now set, will revert on exit.'; pause"
// ...
},
Using a POSIX-like shell (/bin/sh
is npm
's default in Unix-like environments):
"scripts": {
"start": "printf '\\033]0;Custom Title\\a'; echo 'Custom terminal title is now set, will revert on exit.'; printf 'Press ENTER to exit:'; read"
// ...
},
Prompt
function. This would allownpm
or other commands to change the title, but should set it back once done (untested) - about_Prompts – G42 Commented Nov 22, 2024 at 17:57npm
even the restoring-afterward problem doesn't need solving, because the original title is automatically restored whennpm
exits. – mklement0 Commented Nov 26, 2024 at 14:41