I have a couple of scripts written in powershell which are opening a window and showing some text. I would like for every of this windows to have a different background color. At the same time I would like to format the text, which is shown on every window, based on different regexps.
For instance I have 2 windows:
First Window
- I want the background to be gray
- I want the phrase 2024 to be in different color
- I want the phrase eee to be in different color
Second window:
- I want the background to be white
- I want the phrase 2024 to be in different color
- I want the phrase eee to be in different color
I have created a script which suppose to facilitate that but the effect is not correct:
.
Although the background was set to gray, when I start to “colour” the phrases the output is losing the initial background color.
The script is as follows:
function Set-ConsoleColor ($bc, $fc) {
$Host.UI.RawUI.BackgroundColor = $bc
$Host.UI.RawUI.ForegroundColor = $fc
Clear-Host
}
Set-ConsoleColor 'gray' 'white'
$green = [char]27 + "[32m"
$blue = [char]27 + "[34m"
$reset = [char]27 + "[0m"
$content ="2024 bbb ccc ddd eee fff"
$coloredLine = $content -replace "eee", "$green`$&$reset" -replace "2024", "$blue`$&$reset"
Write-Output $coloredLine
Start-Sleep -Seconds 10
How can I achieve effect like in the first or second image?