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).
| Background | Standard Code | Bright Code |
|---|---|---|
| Black | ESC[40m | ESC[100m |
| Red | ESC[41m | ESC[101m |
| Green | ESC[42m | ESC[102m |
| Yellow | ESC[43m | ESC[103m |
| Blue | ESC[44m | ESC[104m |
| Magenta | ESC[45m | ESC[105m |
| Cyan | ESC[46m | ESC[106m |
| White | ESC[47m | ESC[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
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
- 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. - Ensure Contrast: If you use a dark background (Blue, Red), pair it with a light foreground color (Bright White
97, Bright Yellow93). If you use a light background (White, Yellow), pair it with a dark foreground (Black30, Dark Grey90). - Use Padding: Background colors look better with a space on each side of the colored text (e.g.,
SUCCESSinstead ofSUCCESS). This creates visual breathing room. - 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.