There are already several questions about colorizing the PowerShell output, as e.g.:
- How to colorise PowerShell output of Format-Table
With works fine for me with the latest PowerShell 7 version, yet I would like to make my module downwards compatible with Windows PowerShell 5.1 where the color isn’t reset if the concerned colored string is being truncated (e.g. in a table format).
Minimal, Reproducible Example:
$Esc = [Char]0x1b
$Output = [PSCustomObject]@{
Path = 'Person.Address'
Node = "$Esc[36m@{City='New York';..;PostalCode='10021-3100'}$Esc[39m"
Test = 'Optional'
Issue = "$Esc[91m[X] The actual item contains unapproved child items: PostalCode. Expected an item with optional child items listed in @{State=@{..};..;City=@{..}}.$Esc[39m"
}
(Note that the prompt color changed to red, what doesn’t happen if the Issue
property fits the screen)
Is there for PowerShell 5.1 a way to make sure that the ANSI color is always reset for a string (even it is truncated)?