How to Underline or Bold Text Using ANSI Codes in Batch Script
For years, Batch programmers were limited to plain text output. If you wanted to emphasize a word, your only option was to use ALL CAPS or surround it with symbols like ***IMPORTANT***. However, modern Windows terminals (Windows Terminal and updated ConHost on Windows 10/11) now support ANSI Escape Codes, allowing you to apply professional formatting like Bold, Underline, and even Italics directly in your scripts.
In this guide, we will demonstrate how to enable and use these formatting codes to enhance your text output.
Essential ANSI Formatting Codes
To apply formatting, you use a specific code sequence followed by the text you want to format, and you must end the formatted section with the "Reset" code to return to normal.
| Effect | ANSI Code | Notes |
|---|---|---|
| Reset All | ESC[0m | Clears all formatting and colors |
| Bold / Bright | ESC[1m | May display as brighter color rather than bold, depending on the terminal |
| Dim | ESC[2m | Not supported in all terminals |
| Underline | ESC[4m | Widely supported |
| Invert (Negative) | ESC[7m | Swaps foreground and background colors |
Note on Bold vs. Bright: The ESC[1m code is defined as "Bold" in the ANSI standard, but many Windows console implementations render it as a brighter foreground color rather than a heavier font weight. Windows Terminal supports true bold rendering if the configured font has a bold variant. The legacy ConHost typically renders it as bright intensity only.
Setup: Capturing the Escape Character
To use these codes in Batch, you need the literal Escape character (ASCII 27). We store this in a variable to make our script readable.
@echo off
setlocal EnableDelayedExpansion
:: Get real ESC character from PowerShell
for /f %%A in ('powershell -NoProfile -Command "[char]27"') do set "ESC=%%A"
:: Define ANSI styles
set "RESET=!ESC![0m"
set "BOLD=!ESC![1m"
set "UNDERLINE=!ESC![4m"
set "REVERSE=!ESC![7m"
echo This is regular text.
echo !BOLD!This is Bold text.!RESET!
echo !UNDERLINE!This is Underlined text.!RESET!
echo !BOLD!!UNDERLINE!This is Bold AND Underlined text.!RESET!
echo !REVERSE!Highly Important Warning!RESET!
endlocal
pause
Combinations and Nesting
A powerful feature of ANSI codes is that you can combine effects. For example, if you want a bold, underlined, red warning:
set "RED=!ESC![91m"
echo !BOLD!!UNDERLINE!!RED!CRITICAL ERROR DETECTED!RESET!
You can also combine multiple codes into a single escape sequence by separating them with semicolons, which is more concise:
:: Bold (1) + Underline (4) + Bright Red (91) in a single sequence
echo !ESC![1;4;91mCRITICAL ERROR DETECTED!RESET!
Formatting Specific Words in a Sentence
You don't have to format the whole line. You can toggle the formatting on and off for specific words.
echo The status of the server is !BOLD!ONLINE!RESET! and !UNDERLINE!stabilized!RESET!.
Why Use ANSI Bold and Underline?
1. Visual Hierarchy
In a long log output, bolding the timestamps or the service names makes it much faster for a human eye to scan and find specific entries.
2. Error Highlighting
Bolding the word ERROR or FATAL ensures that the user doesn't miss the most critical information amidst a sea of standard text.
3. Professionalism
Underlined headers make your script look like a modern CLI application (like git or kubectl) rather than a legacy 1990s batch file.
Troubleshooting: Why does my text show ←[1m?
If your terminal displays the raw codes (like ←[1m) instead of bolding the text, it means:
- Old Windows: You are on Windows 7 or 8, which do not natively support ANSI in the terminal.
- ANSI Not Enabled: You might need to enable the "Virtual Terminal" feature in the registry for older Windows 10 builds.
The Fix: You can enable ANSI support by running this command once in an elevated command prompt:
reg add HKCU\Console /v VirtualTerminalLevel /t REG_DWORD /d 1 /f
A new terminal session is required after setting this value for the change to take effect.
Summary
The ability to Bold and Underline text via ANSI codes is a game-changer for Batch scripting. It moves your output from a monotone block of text to a structured, professional interface. By defining clear variables for !BOLD! and !RESET!, you can make your scripts significantly more communicative and easier for users to navigate.