admin管理员组

文章数量:1333476

I have a script that opens several PowerShell terminals and runs some commands in each. In order to make it easy for the user to distinguish between the terminals, I'd like to set the background and foreground colors of each. I've got this so far:

start powershell @'
    $host.ui.RawUI.WindowTitle = \"application 1\"
    $host.ui.RawUI.BackgroundColor = \"red\"
    $host.ui.RawUI.ForegroundColor = \"black\"
    cd application1\backend
    npm install
    npm run build
    npm run start
'@

start powershell @'
    $host.ui.RawUI.WindowTitle = \"application 2\"
    $host.ui.RawUI.BackgroundColor = \"blue\"
    $host.ui.RawUI.ForegroundColor = \"white\"
    cd application2\backend
    npm install
    npm run build
    npm run start
'@

start powershell @'
    $host.ui.RawUI.WindowTitle = \"application 3\"
    $host.ui.RawUI.BackgroundColor = \"yellow\"
    $host.ui.RawUI.ForegroundColor = \"black\"
    cd application3\backend
    npm install
    npm run build
    npm run start
'@

What this gives me is the following:

Sure, the background is colored red (or pink), but it seems to be the background of whatever text is logged to the console, not the background of the terminal itself.

I know one can color the full terminal background by going to the terminal properties and selecting Screen Background and the desired color (and similarly for text color):

...but I'd like to know how to do this in the script. I'm assuming whatever can be done in the Properties dialog can be done in script.

Note that it does no good to set the user's PowerShell Profile settings as I don't want to permanently change the user's PowerShell color setting (just for the life of the terminal that opens from the script) and I'm trying to set different colors for each terminal.

What can I add to the script above to accomplish what I want?

Thanks!

I have a script that opens several PowerShell terminals and runs some commands in each. In order to make it easy for the user to distinguish between the terminals, I'd like to set the background and foreground colors of each. I've got this so far:

start powershell @'
    $host.ui.RawUI.WindowTitle = \"application 1\"
    $host.ui.RawUI.BackgroundColor = \"red\"
    $host.ui.RawUI.ForegroundColor = \"black\"
    cd application1\backend
    npm install
    npm run build
    npm run start
'@

start powershell @'
    $host.ui.RawUI.WindowTitle = \"application 2\"
    $host.ui.RawUI.BackgroundColor = \"blue\"
    $host.ui.RawUI.ForegroundColor = \"white\"
    cd application2\backend
    npm install
    npm run build
    npm run start
'@

start powershell @'
    $host.ui.RawUI.WindowTitle = \"application 3\"
    $host.ui.RawUI.BackgroundColor = \"yellow\"
    $host.ui.RawUI.ForegroundColor = \"black\"
    cd application3\backend
    npm install
    npm run build
    npm run start
'@

What this gives me is the following:

Sure, the background is colored red (or pink), but it seems to be the background of whatever text is logged to the console, not the background of the terminal itself.

I know one can color the full terminal background by going to the terminal properties and selecting Screen Background and the desired color (and similarly for text color):

...but I'd like to know how to do this in the script. I'm assuming whatever can be done in the Properties dialog can be done in script.

Note that it does no good to set the user's PowerShell Profile settings as I don't want to permanently change the user's PowerShell color setting (just for the life of the terminal that opens from the script) and I'm trying to set different colors for each terminal.

What can I add to the script above to accomplish what I want?

Thanks!

Share Improve this question asked Nov 20, 2024 at 17:32 Gibran ShahGibran Shah 1,1094 gold badges17 silver badges35 bronze badges 2
  • 3 Just insert a cls (Clear-Host) after setting the BG color. – zett42 Commented Nov 20, 2024 at 19:57
  • You know what? That worked! Thanks zett! – gib65 Commented Nov 21, 2024 at 16:30
Add a comment  | 

1 Answer 1

Reset to default 1

Zett42 has the right answer: I added Clear-Host to the script and that cleared the whole terminal coloring the background the color I set $host.ui.rawui.backgroundcolor.

本文标签: