Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

ANSI Escape codes in PowerShell 5.1

Aug
2,799
144
Further to Expanded ANSI for Subscript and Superscript Not Working

Learned some new things today about ANSI Escape codes and PowerShell under Windows Terminal.

PSStyle handles ANSI Escape codes,
but is only built-into PowerShell 7.x

PSStyle does not exist in Windows PowerShell 5.1,
because it was introduced in PowerShell 7.2 as part of the PSAnsiRendering feature.

To use PSStyle in PowerShell 5.1,
you must install a compatibility module or import a script that creates the variable manually.

I chose the Install-Module route;

Code:
Install-Module PSStyle

$PSStyle is an automatic variable that provides ANSI escape sequences for colors, text styles, and formatting.

It allows you to write things like:

"$($PSStyle.Foreground.Green)H₂O$($PSStyle.Reset)"

Even with the PSStyle installed in PowerShell 5.1,
some $PSStyle features (like RGB rendering) won’t work because they require PowerShell 7.2+.

Code:
$super2 = [char]0x00B2
$sub2   = [char]0x2082

$PSStyle.Foreground.Green + "X$super2 + H$sub2" + "O" + $PSStyle.Reset
X² + H₂O

1783031799189.webp


Thus, subscript and superscript cannot be implemented in PowerShell 5.1 using ANSI escape codes.

It must be done similar to the TCC method;

Code:
echo X%@char[0x00B2] + H%@char[0x2082]O

Joe
 
Note well how PowerShell eliminates the need to code the actual ANSI escape codes for the most common ANSI properties.

1783033043745.webp


This is along the lines of what I was thinking about in my post.

Ref: ^e[0m and all the other !*#@%& ANSI codes

Joe
 
Back
Top