powershellterminalscripting

Why does setting the title of my powershell terminal not stick after other commands are run?


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!


Solution

  • The 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.