How-to: Use ANSI colors in the terminal

ANSI colors are available by default in Windows version 1909 or newer. See below for older versions.

Specify the color codes in a batch file by ECHOing the foreground and/or background COLOR codes (from the following table) followed by the text to be formatted, followed by the ANSI default (Esc[0m) to reset the terminal back to the default colors.
These codes are the same as those used in a Unix/Linux/VT 100 terminal though the colors may be slightly different shades.

ColorForeground Background COLOR equivalent
Default Esc[0m    
Black BLACK Esc[30m Esc[40m 0
Red DARK_RED Esc[31m Esc[41m 4
Green DARK_GREEN Esc[32m Esc[42m 2
Yellow DARK_YELLOW Esc[33m Esc[43m 6
Blue DARK_BLUE Esc[34m Esc[44m 1
Magenta DARK_MAGENTA Esc[35m Esc[45m 5
Cyan DARK_CYAN Esc[36m Esc[46m 3
Light gray DARK_WHITE Esc[37m Esc[47m 7
Dark gray BRIGHT_BLACK Esc[90m Esc[100m 8
Light red BRIGHT_RED Esc[91m Esc[101m C
Light green BRIGHT_GREEN Esc[92m Esc[102m A
Light yellow BRIGHT_YELLOW Esc[93m Esc[103m E
Light blue BRIGHT_BLUE Esc[94m Esc[104m 9
Light magenta BRIGHT_MAGENTA Esc[95m Esc[105m D
Light cyan BRIGHT_CYAN Esc[96m Esc[106m B
White WHITE Esc[97m Esc[107m F
BoldEsc[1m    
UnderlineEsc[4m    
No underlineEsc[24m    
Reverse textEsc[7m    
Positive text (not reversed) Esc[27m    

The Esc character in the above table must be entered with the key combination Ctrl+[
In some text editors this will appear as ^[ or Esc[

Many basic text editors do not provide a method of entering an ANSI Esc character (n.b. do not confuse this with the CMD Escape character) to work around this, either enter Esc at the command line or use an editor like Notepad++ which allows copy and paste.

The actual colors displayed are dependent on the Terminal color scheme which can be set using ColorTool.exe The new default color scheme is called campbell.ini. This was first introduced in Windows 10 clean-install version build 16257 (1709).

The color swatches on this page are based on the new COLOR defaults.

ANSI color codes do also support redirection to a text file. You may then use TYPE to display such an output file complete with colors. However if you want to easily access the raw text without any color codes e.g. for further manipulation/sorting then avoid applying any color codes until after that has been done.

PowerShell 7.2 added a new automatic variable, $PSStyle, and changes to the PowerShell engine to support the output of ANSI-decorated text. See about_ANSI_Terminals for more.

Enabling ANSI colors in older versions of Windows

To use ANSI colours in the Windows terminal requires setting VirtualTerminalLevel.

VirtualTerminalLevel = 1 is now set by default for the terminal and in ConPTY.
In Windows versions 1511 through to 1903 this had to be enabled in the registry at:

[HKEY_CURRENT_USER\Console]
"VirtualTerminalLevel"=dword:00000001

Alternatively it can be enabled by calling the SetConsoleMode API with the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag.

24 bit color

The latest W10 Console has full 24-bit color support, this is primarily for Linux compatibility so the console properties page and the default Windows color mappings still use just 16 colors, there is no plan to add additional console colors to the Win32 API.

To take advantage of the new colors use ColorTool.exe and install a theme, or use the VT100 sequences described here.
When the VT100 sequences are used with a version of the Windows Console that supports 24-bit colors they will display the desired RGB color, older consoles will choose the nearest appropriate color from the existing 16 color table.

Examples

@Echo off
Echo ¤[91m This text will display in Red ¤[0m

In the above ¤ represents the Esc character.

A batch file to display the ANSI colors: EchoANSI.CMD

Output:

ANSI colors

“In nature, light creates the color; in the picture, color creates light.
 Every color shade emanates a very characteristic light — no substitute is possible” ~ Hans Hofmann

Related commands

ECHO - Display a message on screen.
TYPE - Display the contents of a text file.
How-to: 256 Colors for XTerm (Linux)
In MS-DOS ANSI support could be installed by loading ansi.sys at startup.
ConPTY - Blog post describing the Windows Pseudo Console.
Console Virtual Terminal Sequences - Full list of VT 100 codes, positioning, colors, modes, RGB (for non Windows consoles).
Dracula Theme - A dark theme for the Windows 10 Console.
WT.exe - Windows Terminal
CSS/HTML Colour codes


 
Copyright © 1999-2024 SS64.com
Some rights reserved