Skip to main content

How to Print Colored Backgrounds in Console Output in Batch Script

Adding color to the text foreground is a common way to highlight information, but using Colored Backgrounds (often called "Highlighting") provides an even stronger visual cue. It allows you to create "Status Badges" (e.g., a green bar behind the word "SUCCESS") or call-out boxes that are impossible to miss even in a busy terminal window.

In this guide, we will demonstrate how to use ANSI Escape Codes to apply background colors to your Batch script output.

The Logic: ANSI Background Codes

Background colors in the Windows terminal use codes derived from the SGR (Select Graphic Rendition) standard. The codes for backgrounds are in the range of 40–47 (standard) and 100–107 (bright).

BackgroundStandard CodeBright Code
BlackESC[40mESC[100m
RedESC[41mESC[101m
GreenESC[42mESC[102m
YellowESC[43mESC[103m
BlueESC[44mESC[104m
MagentaESC[45mESC[105m
CyanESC[46mESC[106m
WhiteESC[47mESC[107m

Setup: Defining Color Variables

To make your script readable, you should always capture the Escape character and define color variables at the beginning of your file.

@echo off
setlocal enabledelayedexpansion

:: Get ESC character reliably (ASCII 27) via PowerShell
for /f %%A in ('powershell -NoProfile -Command "[char]27"') do set "ESC=%%A"

if not defined ESC (
echo [ERROR] Could not generate ANSI escape character.
pause
exit /b 1
)

:: Define Background Colors
set "BG_RED=!ESC![41m"
set "BG_GREEN=!ESC![42m"
set "BG_YELLOW=!ESC![43m"
set "BG_BLUE=!ESC![44m"
set "BG_WHITE=!ESC![47;30m"
set "RESET=!ESC![0m"

:: Define Foreground + Background Combinations
set "SUCCESS=!ESC![1;97;42m"
set "DANGER=!ESC![1;97;41m"

echo.
echo Standard text followed by:
echo !SUCCESS! SUCCESS !RESET! - Task completed.
echo !DANGER! FATAL ERROR !RESET! - System shutdown initiated.
echo.
echo !BG_BLUE!!ESC![97m This entire line has a blue background with white text. !RESET!
echo.

endlocal
pause

How the combined codes work:

  • ESC[1;97;42m: This single escape sequence applies three attributes at once, separated by semicolons: Bold (1), Bright White foreground (97), and Green background (42).
  • ESC[47;90m: White background (47) with Dark Grey foreground (90), ensuring readable contrast.
  • ESC[0m (Reset): Clears all formatting, both foreground and background, returning to the terminal's default colors.

Creating "Status Badges"

The most professional use of background colors is creating small badges that signify the status of a process.

@echo off
setlocal enabledelayedexpansion

:: Get ESC reliably via PowerShell (works on Win10/11)
for /f %%A in ('powershell -NoProfile -Command "[char]27"') do set "ESC=%%A"

if not defined ESC (
echo [WARNING] ANSI not available. Badges will display without color.
set "C_OK="
set "C_WAIT="
set "C_FAIL="
set "C_RESET="
) else (
set "C_OK=!ESC![1;97;42m"
set "C_WAIT=!ESC![1;30;43m"
set "C_FAIL=!ESC![1;97;41m"
set "C_RESET=!ESC![0m"
)

echo Checking Services...
echo.

echo [!C_OK! OK !C_RESET!] Windows Update
echo [!C_WAIT! WAIT !C_RESET!] Background Intelligent Transfer Service
echo [!C_FAIL! FAIL !C_RESET!] Distributed Transaction Coordinator
echo.

endlocal
pause
info

Padding for visual balance: Notice the spaces inside the badges ( OK , WAIT, FAIL). Adding padding on both sides of the text within the colored region makes badges look cleaner and more balanced. Without padding, the colored background hugs the text too tightly and looks cramped.

Comparisons: Background vs. Foreground-Only

  • Foreground Only: Best for standard logging where you just want to categorize information (e.g., blue for info, yellow for warning). It is subtler and less visually disruptive.
  • Background Highlighting: Best for "Final States" and critical alerts. If a script crashes, a red background tells the user immediately that something is wrong, even if they aren't looking directly at the text.

Best Practices

  1. Always Reset: If you forget to use !RESET!, the background color will spill over into the next line and potentially fill the entire rest of the terminal window with that color.
  2. Ensure Contrast: If you use a dark background (Blue, Red), pair it with a light foreground color (Bright White 97, Bright Yellow 93). If you use a light background (White, Yellow), pair it with a dark foreground (Black 30, Dark Grey 90).
  3. Use Padding: Background colors look better with a space on each side of the colored text (e.g., SUCCESS instead of SUCCESS). This creates visual breathing room.
  4. Graceful Degradation: These colors require Windows 10 (Build 1511+) or Windows 11. On older systems, the raw escape codes will be displayed as garbled text. Always check whether the ESC variable was captured and fall back to plain text when it is not available.

Conclusion

Colored backgrounds are a powerful UI tool that brings a "web-like" or "dashboard-like" clarity to the command line. By using them to create high-contrast badges and call-outs, you ensure that your Batch scripts communicate their most critical information instantaneously, improving both the professionalism and the safety of your automation tools.