How to echo with different colors in the Windows command line

Learn how to echo with different colors in the windows command line with practical examples, diagrams, and best practices. Covers windows, batch-file, command-line development techniques with visua...

Echoing with Style: Adding Color to Your Windows Command Line

Hero image for How to echo with different colors in the Windows command line

Learn how to inject vibrant colors into your Windows command line output using various techniques, from simple echo commands to more advanced batch scripting.

The standard Windows Command Prompt (CMD) is notoriously monochromatic, often making it difficult to quickly parse different types of information. Unlike Linux/Unix terminals, CMD doesn't natively support ANSI escape codes for color. However, with a few clever tricks and external tools, you can bring a splash of color to your batch scripts and command-line output. This article will guide you through several methods to achieve colored text in the Windows command line, enhancing readability and making your scripts more engaging.

Understanding the Challenge: ANSI Escape Codes

Most modern terminals, especially in Unix-like environments, support ANSI escape codes. These are special sequences of characters that, when printed, are interpreted by the terminal as commands to change text attributes like color, boldness, or background. The Windows Command Prompt, by default, does not interpret these codes, leading to them being printed literally as gibberish characters. This is the primary hurdle we need to overcome.

flowchart TD
    A[User wants colored output] --> B{Is it a Linux/macOS terminal?}
    B -- Yes --> C[Use ANSI Escape Codes]
    B -- No (Windows CMD) --> D{Does CMD support ANSI?}
    D -- No (Default) --> E[Problem: Codes print literally]
    D -- Yes (Windows 10+ with VT) --> F[Enable Virtual Terminal Processing]
    E --> G[Solution 1: External Tools (e.g., ANSICON)]
    E --> H[Solution 2: PowerShell]
    F --> I[Use ANSI Escape Codes directly]
    G --> J[Colored Output]
    H --> J
    I --> J

Decision flow for achieving colored output in different terminal environments.

Method 1: Using color Command (Limited Options)

The simplest way to change colors in the Windows Command Prompt is using the built-in color command. This command changes the foreground and background colors of the entire console window, not individual lines or words. It's useful for setting a theme for your script but lacks fine-grained control.

@echo off
REM Change console background to black (0) and foreground to light green (A)
color 0A

echo This text is light green on a black background.
pause > nul

REM Change to light red (C) foreground
color 0C
echo This text is light red on a black background.
pause > nul

REM Reset to default (usually white on black)
color
echo Back to default colors.
pause > nul

Basic usage of the color command in a batch script.

Method 2: Enabling Virtual Terminal Processing (Windows 10+)

Starting with Windows 10, Microsoft added support for Virtual Terminal (VT) sequences, which include ANSI escape codes. This feature isn't enabled by default for all applications but can be activated programmatically within your C/C++ or PowerShell scripts. For batch files, you can leverage PowerShell to enable it temporarily or use a small helper executable.

@echo off

REM Check if running on Windows 10 or later
ver | findstr /i "Microsoft Windows \[Version 10\." > nul
if %errorlevel% neq 0 (
    echo This method requires Windows 10 or later.
    goto :eof
)

REM Enable Virtual Terminal Processing using PowerShell
powershell -Command "$host.UI.RawUI.ForegroundColor = 'White'; $host.UI.RawUI.BackgroundColor = 'Black'; [console]::OutputEncoding = [System.Text.Encoding]::UTF8; $h = Get-StdHandle -StdOutputHandle; $m = Get-ConsoleMode $h; Set-ConsoleMode $h ($m -bor 0x0004);"

REM Now you can use ANSI escape codes directly
echo This is red text.

echo This is green text.

echo This is blue text.

echo This is bold yellow text.

echo This is underlined magenta text.

echo White text on red background.

echo Back to default.

pause > nul

REM Reset console mode (optional, but good practice)
powershell -Command "$h = Get-StdHandle -StdOutputHandle; $m = Get-ConsoleMode $h; Set-ConsoleMode $h ($m -band -bnot 0x0004);"

Using PowerShell to enable VT processing and then printing ANSI colored text in a batch file.

Method 3: Using PowerShell (Native Support)

PowerShell, being a more modern and powerful shell, has native support for colored output without needing to enable VT processing explicitly for basic foreground/background changes. It offers cmdlets like Write-Host with -ForegroundColor and -BackgroundColor parameters, providing a much cleaner syntax.

PowerShell Script

Write-Host "This is default text." Write-Host "This is red text." -ForegroundColor Red Write-Host "This is green text." -ForegroundColor Green Write-Host "This is blue text." -ForegroundColor Blue Write-Host "This is yellow text with a red background." -ForegroundColor Yellow -BackgroundColor Red Write-Host "" Write-Host "Using ANSI escape codes directly (also works in PowerShell):" "e[31mThis is red text via ANSIe[0m" "e[32mThis is green text via ANSIe[0m" "e[1;33mThis is bold yellow text via ANSIe[0m"

Batch calling PowerShell

@echo off

REM Call PowerShell to print colored text powershell -Command "Write-Host 'This is red text from batch.' -ForegroundColor Red" powershell -Command "Write-Host 'This is green text from batch.' -ForegroundColor Green" powershell -Command "Write-Host 'This is yellow text with a blue background from batch.' -ForegroundColor Yellow -BackgroundColor Blue"

pause > nul

Method 4: Using External Tools (e.g., ANSICON)

For older Windows versions or if you prefer a more universal solution that doesn't rely on Windows 10 VT support or PowerShell, external utilities like ANSICON can be used. ANSICON is a small utility that hooks into the console API and translates ANSI escape codes into Windows console API calls, effectively enabling ANSI support for any application run through it.

1. Download ANSICON

Download the appropriate version of ANSICON (32-bit or 64-bit) from its GitHub repository or a trusted source. Extract the contents to a convenient location, for example, C:\ANSICON.

Open an administrator command prompt, navigate to the ANSICON directory (e.g., cd C:\ANSICON\x64), and run ansicon.exe -i. This installs ANSICON as a system-wide hook, meaning any subsequent CMD window will automatically support ANSI escape codes.

3. Use ANSI Escape Codes

After installation, you can directly use ANSI escape codes in your batch files, and ANSICON will interpret them. If you don't install it system-wide, you can prefix your commands with ansicon.exe (e.g., ansicon.exe echo Red Text).

@echo off

REM This assumes ANSICON is installed system-wide or ansicon.exe is in PATH
REM Or, if not installed, you would use: C:\ANSICON\x64\ansicon.exe echo Red Text

echo This text is red.
echo This text is green.
echo This text is yellow.
echo This is bold cyan text.
echo White text on blue background.
echo Back to normal.

pause > nul

Example of using ANSI escape codes after ANSICON installation.

By employing these methods, you can significantly improve the visual clarity and user experience of your Windows command-line scripts. Whether you opt for the simple color command, leverage modern Windows 10 VT processing, utilize the power of PowerShell, or rely on external tools like ANSICON, adding color makes your output more informative and aesthetically pleasing.