Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Show ANSI Samples

Posted on May 29, 2020October 1, 2020

Earlier this week I did a live session for the PSPowerHour. I talked about ways to dress up your PowerShell work and console sessions.  One of things I talked about was using ANSI escape sequences  to add some color.  What I realized was that I was constantly referring to my reference source at https://en.wikipedia.org/wiki/ANSI_escape_code.  That's good up to a point, but I still need to see what a value looks like in my console to know if I want to use it. I don't know why I didn't do this sooner but I wrote a quick and dirty script to display ANSI sequences from a PowerShell prompt. There are others in the wild if you don't like this one.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!
#requires -version 5.1
#requires -module PSScriptTools

#Show-ANSI.ps1
#comment out the Add-Border lines if you aren't using the PSScriptTools module.
[cmdletbinding()]
Param(
    [switch]$Basic,
    [switch]$Foreground,
    [switch]$Background,
    [int[]]$RGB
)

# a private function to display results in columns on the screen
Function display {
Param([object]$all,[int]$Max)

    $c = 1
    for ($i=0;$i -le $all.count;$i++) {
        if ($c -gt $max) {
            Write-Host "`r"
            $c=1
        }
        Write-Host "$($all[$i]) `t" -NoNewline
        $c++
    }
}

if ($IsCoreCLR) {
    $esc = "`e"
    $escText = '`e'
    $max = 3
}
else {
    $esc = $([char]0x1b)
    $escText = '$([char]0x1b)'
    $max = 2
}

#basic
if ($basic) {

    Add-Border "Basic Sequences" -ansitext "$esc[1m"

    $basichash = @{
        1 = "Bold"
        2 = "Faint"
        3 = "Italic"
        4 = "Underline"
        5 = "SlowBlink"
        6 = "RapidBlink"
        7 = "Reverse"
        9 = "CrossedOut"
    }

    $basichash.GetEnumerator() | ForEach-Object {
        $sequence = "$esctext[$($_.name)m$($_.value)$esctext[0m"
        #"$esc[$($_.name)m$($_.value)$esc[0m"
        "$esc[$($_.name)m$sequence$esc[0m"
    }
}

If ($Foreground) {

    Add-Border "Foreground" -ansitext "$esc[93m"
    $n = 30..37
    $n+= 90..97

    $all = $n | ForEach-Object {
         $sequence = "$esctext[$($_)mHello$esctext[0m"
        "$esc[$($_)m$sequence$esc[0m"
    }

    display -all $all -Max $max

    Write-Host "`n"
    Add-Border "8-Bit Foreground" -ansitext "$esc[38;5;10m"
    $all = 1..255 | ForEach-Object {
         $sequence = "$esctext[38;5;$($_)mHello$esctext[0m"
        "$esc[38;5;$($_)m$sequence$esc[0m"
    }
      display -all $all -Max $max
}

If ($Background) {

    Add-Border "Background" -ansitext "$esc[46m"
    $n = 40..47
    $n+= 100..107
    $all = $n | ForEach-Object {
         $sequence = "$esctext[$($_)mHello$esctext[0m"
        "$esc[1;$($_)m$sequence$esc[0m"
    }

    display -all $all -Max $max

    Write-Host "`n"
    Add-Border "8-Bit Background" -ansitext "$esc[7;38;5;10m"
    $all = 1..255 | ForEach-Object {
         $sequence = "$esctext[48;5;$($_)mHello$esctext[0m"
        "$esc[1;48;5;$($_)m$sequence$esc[0m"
    }
    display -all $all -Max $max
}

if ($RGB) {

    if ($rgb.count -ne 3) {
        Write-Warning "Wrong number of arguments. Specify RED,GREEN and BLUE values"
        return
    }
    $test = $rgb | Where-object {$_ -gt 255}
    if ($test.count -gt 0) {
        Write-Warning "Detected a value above 255"
        return $rgb
    }

    $r = $rgb[0]
    $g = $rgb[1]
    $b = $rgb[2]

    $sequence = "$esctext[38;2;$r;$g;$($b)m256 Color (R:$r)(G:$g)(B:$b)$esctext[0m"
    "$esc[38;2;$r;$g;$($b)m$sequence$esc[0m"

}

As I said, this is a "quick and dirty" solution. The intention was that you could run it in a Windows PowerShell or PowerShell 7 session and see exactly what a sequence looks like on your system. The output will include the exact sequence you would use depending on your PowerShell version.

ansi-basic

You can test out basic settings. Not every setting will work on every system. For me, it looks like I could use Reverse, Underline and Bold.

You can see Foreground options.

ansi-fg

Background options:

ansi-bg

I thought about displaying every RGB combination but realized that would quickly get out of hand. Instead you can specify an array of values in the order of R,G,B.

ansi-rgbAs you can see, or actually can't, some sequences work better than others depending on your session. Which is one of the reasons I wrote this.

I hope you find it useful.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d