'List of all colors available for PowerShell?

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"


Solution 1:[1]

Pretty grid

$colors = [enum]::GetValues([System.ConsoleColor])
Foreach ($bgcolor in $colors){
    Foreach ($fgcolor in $colors) { Write-Host "$fgcolor|"  -ForegroundColor $fgcolor -BackgroundColor $bgcolor -NoNewLine }
    Write-Host " on $bgcolor"
}

screenshot of colourful output

Updated colours in newer powershell:

screenshot of colourful output

https://gist.github.com/timabell/cc9ca76964b59b2a54e91bda3665499e

Solution 2:[2]

I've found it useful to preview how the console colors will display with a simple helper function:

function Show-Colors( ) {
  $colors = [Enum]::GetValues( [ConsoleColor] )
  $max = ($colors | foreach { "$_ ".Length } | Measure-Object -Maximum).Maximum
  foreach( $color in $colors ) {
    Write-Host (" {0,2} {1,$max} " -f [int]$color,$color) -NoNewline
    Write-Host "$color" -Foreground $color
  }
}

Solution 3:[3]

How about checking the help? Like so, get-help write-host will tell you:

[-BackgroundColor {Black | DarkBlue | DarkGreen | DarkCyan | DarkRed | DarkMagenta | DarkYellow | Gray | DarkGray | Blue | Green | Cyan | Red | Magenta | Yellow | White}]
[-ForegroundColor {Black | DarkBlue | DarkGreen | DarkCyan | DarkRed | DarkMagenta | DarkYellow | Gray | DarkGray | Blue | Green | Cyan | Red | Magenta | Yellow | White}]

Solution 4:[4]

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

Solution 5:[5]

Here is an example of displaying all color combinations of background and foreground colors.

$FGcolors = [enum]::GetValues([System.ConsoleColor])
$BGcolors = [enum]::GetValues([System.ConsoleColor])

Foreach ($FGcolor in $FGcolors)
{
    Foreach ($BGcolor in $BGcolors)
    {
        Write-Host ("Foreground: $FGColor BackGround: $BGColor")  -ForegroundColor $FGcolor -BackgroundColor $BGcolor
    }
}

Solution 6:[6]

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

Solution 7:[7]

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:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Emperor XLII
Solution 3 vonPryz
Solution 4 Hashbrown
Solution 5 ruffin
Solution 6 ruffin
Solution 7