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 |1 Answer
Reset to default 1Zett42 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
.
本文标签:
版权声明:本文标题:How do I set foreground and background colors permanently in a script that opens several new PowerShell terminals? - Stack Overf 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742340857a2456574.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
cls
(Clear-Host
) after setting the BG color. – zett42 Commented Nov 20, 2024 at 19:57