65

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 8

96

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

2
  • 4
    This code shows very well that DarkYellow actually isn't that dark and neither very yellow. Is this a bug?
    – Meiswjn
    Aug 20, 2019 at 6:45
  • 1
    DarkYellow and DarkMagenta background look very different (but correct) in powershell 6.1+.
    – not2qubit
    May 24, 2020 at 9:40
67

The console colors are in an enum called [System.ConsoleColor]. You can list all the values using the GetValues static method of [Enum]

[Enum]::GetValues([System.ConsoleColor])

or just

[Enum]::GetValues([ConsoleColor])
2
  • 21
    Just expanding on mjolinor's code:[Enum]::GetValues([System.ConsoleColor]) | foreach { Write-Host "$_" -ForegroundColor $_} This will display all the names of the colors in the said color Jul 20, 2016 at 17:38
  • 1
    In PowerShell 5-7 I had to use: [System.Enum]::GetValues([System.ConsoleColor]) | foreach { Write-Host "$_" -ForegroundColor $_} Dec 14, 2020 at 4:36
25

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
  }
}
11

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}]
1
  • 1
    I used the get-gelp for forgroundcolor and that brought me to powershell profiles. thank you anyway Dec 12, 2013 at 11:10
9

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

3
  • Sadly this doesn't work at all in powershell 6.1.1 on Win8.1.
    – not2qubit
    May 24, 2020 at 9:50
  • sorry, 5.1.18362.752 (build 10.0.18362.752) on Win10 here
    – Hashbrown
    May 24, 2020 at 10:59
  • I've read the linked material but somehow, I fail to see how to achieve what I want. I'd like to Write-Host -ForegroundColor ??? so that the text is e.g. pink. In fact, I'd like to specify my personal triple of integers to use a custom RGB color. It seems that your answer suggests it's possible but I haven't grasp the how-to of it. Oct 4, 2020 at 10:11
4

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
    }
}
1
  • 1
    The output can be compacted, for quicker visual scanning: Write-Host "$FGColor on $BGColor " -background $BGColor -foreground $FGColor -nonewline.
    – joharr
    Aug 26, 2016 at 7:46
3

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://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-host?view=powershell-7#parameters

2

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:

3
  • UPDATE: Apparently the PresentationFramework is not available until the GetTypes method has been executed, so in order to do that we have to add the -PassThru like this: Add-Type -AssemblyName PresentationFramework -PassThru | Out-Null. See discussion.
    – not2qubit
    Feb 6, 2022 at 14:12
  • LOL, I didn't come here for this, but now that I've seen it I can't stop thinking about it.
    – Ro Yo Mi
    Oct 8, 2022 at 13:16
  • 1
    UPDATE-2: Apparently the PresentationFramework is problematic these days. Instead you may need to load the PresentationCore instead, using: Add-Type -AssemblyName PresentationCore -PassThru | Out-Null and/or using: [System.Reflection.Assembly]::LoadFrom("$PSHome\PresentationCore.dll").GetTypes() | Out-Null. I have no idea what is happening there.⁉️
    – not2qubit
    Dec 9, 2022 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.