Skip to main content

How to Change the Command--Prompt Colors in Batch Script

Customizing the appearance of the command prompt window can make your batch scripts more user-friendly and visually appealing. A splash of color can be used to draw attention to warnings, highlight success messages, or simply give your script a unique identity. The standard, built-in command for managing the console's foreground and background colors is the COLOR command.

This guide will teach you how to use the COLOR command, explain the simple hexadecimal color codes, and show you practical examples of how to set, change, and restore colors to create more engaging and professional-looking scripts.

The Core Command: COLOR

The COLOR command is a simple utility that sets the default foreground and background colors for the entire command prompt window.

The syntax is: COLOR [BF]

  • [BF]: A two-digit hexadecimal code.
    • B: The first digit specifies the Background color.
    • F: The second digit specifies the Foreground (text) color.

The Color Codes

The colors are represented by single hexadecimal digits (0-9 and A-F). You can see the full list by running COLOR /? or HELP COLOR in a command prompt.

CodeColorCodeColor
0Black8Gray
1Blue9Light Blue
2GreenALight Green
3AquaBLight Aqua
4RedCLight Red
5PurpleDLight Purple
6YellowELight Yellow
7WhiteFBright White

Basic Example: Setting a New Color Scheme

Let's change the console from the default black background and white text to a classic "Matrix" style of black background and light green text.

  • Background: Black -> Code 0
  • Foreground: Light Green -> Code A
  • Combined Code: 0A
@ECHO OFF
ECHO Changing the color scheme to Light Green on Black...
COLOR 0A

ECHO.
ECHO This is the new color scheme.
ECHO It will persist until the window is closed or it is changed again.

When you run this, the entire window will instantly change color.

Another Example: Blue Background, Yellow Text

  • Background: Blue -> Code 1
  • Foreground: Light Yellow -> Code E
  • Combined Code: 1E
COLOR 1E

Restoring the Default Colors

If you change the color in your script, it's good practice to restore the original colors before your script exits. The standard default is gray/white text on a black background.

  • Background: Black -> Code 0
  • Foreground: White/Gray -> Code 7
  • Combined Code: 07

You can also restore the original colors by running the COLOR command with no arguments.

@ECHO OFF
ECHO Current colors are the default.
ECHO.
PAUSE

ECHO Changing to Red on White for a warning...
COLOR FC

ECHO [WARNING] This is a critical message!
ECHO.
PAUSE

ECHO Restoring the default colors...
COLOR
ECHO The colors are now back to normal.

Common Pitfalls and How to Solve Them

Problem: The Color Change is Permanent

When you use the COLOR command, the change affects the entire command prompt session and will remain even after your script has finished. If a user runs your script, their prompt might be left with an unusual color scheme.

Solution: Use SETLOCAL and ENDLOCAL

This is the most professional way to handle temporary state changes. The SETLOCAL command saves the current state of the environment, including the console colors. When ENDLOCAL is called (or when the script ends), the original state is automatically restored.

@ECHO OFF
SETLOCAL
ECHO --- My Script ---
COLOR 1E
ECHO This script uses a custom color scheme.
ECHO ...
ECHO Script is finished.
ENDLOCAL
REM The colors are automatically restored here.

This ensures your script is a "good citizen" and doesn't permanently alter the user's environment.

Problem: Choosing an Invalid Color Code

If you provide the same digit for both the background and foreground, the command will fail. You cannot have red text on a red background.

Example of script with error:

C:\> COLOR 44
Invalid color attribute.

Solution: Always choose two different hexadecimal digits for your color code.

Practical Example: A Script with Status Colors

This script uses color changes to provide visual feedback to the user about the status of its operations.

@ECHO OFF
SETLOCAL
TITLE System Health Check

:CheckServices
ECHO --- Checking System Services ---
ECHO.
COLOR 0B
ECHO Checking the 'Spooler' service...
REM (Imagine service check logic here)
ECHO [OK] The Print Spooler service is running.
ECHO.
PAUSE

:CheckConnection
ECHO --- Checking Network Connection ---
ECHO.
COLOR 0E
ECHO Pinging the gateway...
PING -n 1 192.168.1.1 > NUL
IF %ERRORLEVEL% EQU 0 (
COLOR 0A
ECHO [SUCCESS] The gateway is online.
) ELSE (
COLOR 0C
ECHO [FAILURE] The gateway is offline!
)
ECHO.
PAUSE

ECHO --- Check Complete ---
ENDLOCAL
note

This script uses different colors to represent different stages and outcomes, making its output much easier to interpret at a glance.

Conclusion

The COLOR command is a simple but effective tool for improving the user experience of your batch scripts. A well-placed color change can make your scripts feel more professional and can effectively draw attention to important messages.

Key takeaways for using COLOR:

  • Use the COLOR BF syntax, where B is the background hex code and F is the foreground hex code.
  • Run COLOR with no arguments to restore the default black and white theme.
  • Wrap your script's logic in SETLOCAL and ENDLOCAL to ensure that your color changes are temporary and the user's original console colors are restored when your script is done.
  • Use color to highlight success (green), warnings (yellow), and errors (red) to make your script's output more intuitive.