Search notes:

PowerShell: ANSI colors

Foreground and background colors

set-strictMode -version latest

$CSI =  "$([char] 27)" + "["

"normal "      +
$CSI           +
  "9"          + # high intensity foregroud
  "2"          + # green
  "m"          +
"colored text" +
"$CSI"         +
"m"            + # reset
" normal again"

"normal "      +
$CSI           +
  "4"          + # normal intensity background
  "2"          + # green
  "m"          +
"colored text" +
"$CSI"         +
"m"            + # reset
" normal again"
Github repository about-PowerShell, path: /host/ANSI/foreground-background-16.ps1

Animated colors

set-strictMode -version latest

$CSI="$([char]27)[" # PowerShell 6 has `e for [char]27

"${CSI}2J" # Clear screen


while (1) {
   foreach ($r_ in -255 .. 255) {

      $r = [Math]::Abs($r_)

      $g = 255-$r
      "${CSI}10;15H"          + # Move to column 10, row 15
      "${CSI}38;2;$r;$g;200m" + # Set foreground coor (38;2) to RGB($r, $g, 200)
      "Foo, bar and baz!"       # Write some text

      start-sleep -Milliseconds 10
   }
}
Github repository about-PowerShell, path: /host/ANSI/animated.ps1

Blinking text

The code 5 causes text to blink, but does not work in all terminals or console such as for example cmd.exe. It does, however, work in the Windows Terminal (wt.exe):
clear-host

$CSI = "$([char]27)["

"

    ${CSI}5;31mnormal read ${CSI}5;91mintense red${CSI}m

"
Github repository about-PowerShell, path: /host/ANSI/blink.ps1
The blink turn of code is 25.

Bold and underlined text

$CSI = "$([char]27)["

"${CSI}1mbold${CSI}m and ${CSI}4munderlined${CSI}m"
Github repository about-PowerShell, path: /host/ANSI/bold-underlined.ps1
The codes to turn of bold and underline are 22 and 24.

See also

Colors
Coloring the output of a PowerShell command.
The .NET class System.Managemenet.Automation.VTUtility
ANSI escape sequences
SQL*Plus/Powershell: Color error messages when running a script

Index