powershell

How to add color to symbol objects?


I am looking to provide users with a size list that also compares against a threshold. if threshold is exceeded, that should generate a RED X mark. Otherwise, if threshold is not exceeded that should generate a green checkmark

I found this code:

$greenCheck = @{
  Object = [Char]8730
  ForegroundColor = 'Green'
  NoNewLine = $true
  }
Write-Host @greenCheck

That is fantastic but requires write-host... I can't just call it like this in other words @greenCheck

The splatting operator '@' cannot be used to reference variables in an expression. '@greenCheck' can be used only as an argument to a command. To reference variables in an expression use '$greenCheck'.

I found this other code here

$symbols = [PSCustomObject] @{
    SMILEY_WHITE = ([char]9786)
    SMILEY_BLACK = ([char]9787)
    GEAR = ([char]9788)
    HEART = ([char]9829)
    DIAMOND = ([char]9830)
    CLUB = ([char]9827)
    SPADE = ([char]9824)
    CIRCLE = ([char]8226)
    NOTE1 = ([char]9834)
    NOTE2 = ([char]9835)
    MALE = ([char]9794)
    FEMALE = ([char]9792)
    YEN = ([char]165)
    COPYRIGHT = ([char]169)
    PI = ([char]960)
    TRADEMARK = ([char]8482)
    CHECKMARK = ([char]8730)
}

This one is perfect! but it doesn't have colors. How can I add colors to the object? Also, is there more symbols I can find like an X mark? Or would I have to just use good old 'X' char?


Solution

  • For a green checkmark and a red x try: "$([char]0x1b)[92m$([char]8730) $([char]0x1b)[91m×" enter image description here

    Some Links:
    https://emojipedia.org/microsoft/windows-10-october-2018-update/cross-mark/
    How can I get Mocha's Unicode output to display properly in a Windows console?

    Ansi escape codes

    The new Terminal allows change of the font size like other apps with CTRL+Wheel among other goodies.