List of all colors available for PowerShell?

Viewed 77831

I am searching for a list of all colors I can use in PowerShell. Since we need to provide names and no hexnumbers, it's hard to figure out if a color exists or not, at least if you don't know how :))

For example, as -foregroundcolor

write-host "hello world" -foregroundcolor "red"
8 Answers

I'm not going to write down all ~7million (which you can apparently use now if your terminal can display them), but here are the main ones, all named for you
I've included other things like "bold", underline, and negative.

Just call them like this (fg for foreground, bg for background, and bf/bg for "bright" foreground/background. default to reset and there's fg.default + bg.default too for resetting those individually)

$style.fg.green + 'Im green!'
'I feel a little ',$style.bg.black,' moody' -join ''
"Math is pretty $($style.negative)$(191 * 7)$($style.default) too"

COLOURS!

Those 24-bit colours a aluded to? $style.bg.rgb -f 120,32,230. Maybe you're running pwsh on linux or come from a 'nix background? $style.fg.x -f 30 xterm colours will make you feel at home.

#console colours (VT escape sequences)
$style=@(
    0, 'default', 'bold',
    4, 'underline',
    24, 'nounderline',
    7, 'negative',
    27, 'positive',
    '_fg',
        30, 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white',
        39, 'default',
    '_bg',
        'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white',
        49, 'default',
    '_bf', 90, 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white',
    '_bb', 100, 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'
) `
| &{
    Begin {
        $sequence="$([char]27)[{0}m"
        $style=@{
            fg=@{
                rgb=$sequence -f '38;2;{0};{1};{2}'
                x=$sequence -f '38;5;{0}'
            };
            bg=@{
                rgb=$sequence -f '48;2;{0};{1};{2}'
                x=$sequence -f '48;5;{0}'
            };
            bf=@{};
            bb=@{};
        }
        $current=$style
        $index=$null
    }
    Process {
        Switch -regex ($_) {
            '\d' { $index=$_ }
            '^_' { $current=$style[$_ -replace '^.',''] }
            Default {
                $current[$_]=$sequence -f $index++
#comment me out
"$($current[$_])$($_)`t" | Out-Host
            }
        }
    }
    End {
        $style
#more demonstrations
@(($style.bg.rgb -f 120,32,230), ($style.fg.x -f 30), 'hello', $style.default) -join '' | Out-Host
    }
}

Apparently you can set hyperlinks and truncated text too!
https://github.com/PowerShell/PowerShell/issues/7744

It doesn't have to be that hard. Tab completion is your friend. Pressing tab after -foregroundcolor (or any unique abbreviation) will list them. In emacs edit mode, they will all list at once.

set-psreadlineoption -editmode emacs  # put in your $profile

write-host hello world -f # press tab, it actually appears above it

Black      Cyan       DarkCyan   DarkGreen   DarkRed    Gray      Magenta    White
Blue       DarkBlue   DarkGray   DarkMagenta DarkYellow Green     Red        Yellow

It's also in the docs under -Foregroundcolor (and -BackgroundColor):

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-host?view=powershell-7#parameters

In case you come here from Google and want to know what color names are available in Windows, you can do:

Add-Type –assemblyName PresentationFramework; [System.Windows.Media.Colors] | Get-Member -static -Type Property |Select -Expand Name

(Notice how you first need to load the PresentationFramework assembly.)

AliceBlue
AntiqueWhite
Aqua
Aquamarine
Azure
Beige
Bisque
Black
BlanchedAlmond
Blue
BlueViolet
Brown
BurlyWood
CadetBlue
Chartreuse
...

Only some of these are printable by their name in the Powershell Console, although the new console supports "all" (24-bit TRUE) colors, through the ANSI color escape sequences and when VT has been enabled.(VT = Console Virtual Terminal Support)

To get a list of the (Write-Host) printable names, you can do this:

# Add-Type –assemblyName PresentationFramework
$colors = [System.Windows.Media.Colors] | Get-Member -static -Type Property |Select -Expand Name
Foreach ($col in $colors) { try { Write-Host "$col"  -ForegroundColor $col -BackgroundColor Black } catch {} }

enter image description here

To get a full list of all the Windows defined color names, you can run this magic:

function argb2box { $c = ($args[0] -split '#..(.{2})(.{2})(.{2})'); $r,$g,$b = ("$c" -join(' ')).trim() -split ' '; return "`e[48;2;$([Int32]"0x$r");$([Int32]"0x$g");$([Int32]"0x$b")m    `e[0m"; }
$sc=[System.Windows.Media.Colors]; $sc | Get-Member -static -Type Property |Select -Expand Name| ForEach { [pscustomobject] @{ ARGB = "$($sc::$_)"; MEOW = $(argb2box "$($sc::$_)") ; Color = $_}}

# You can sort on HEX values by adding:
# | sort -Property ARGB

enter image description here

enter image description here


You can read more about this here:

Related