Color in Batch Script

MD Aminul Islam May 24, 2022
  1. Color Codes in Batch
  2. Examples to Change Color in Batch
Color in Batch Script

Suppose you got bored seeing the black and white screen of the Command Prompt. You can follow this article to colorize your program screen.

This article will teach how to include color in our program to make some important words focus on users.

The keyword COLOR is used to add color to the program. It is an internal command.

The general format for this command is COLOR [BACKGROUND_COLOR][FOREGROUND_COLOR]. The attributes for color are specified by 2 of the following hex digits.

The most important part here is that there should be no space between two-color numbers. Color digits are specified below.

Color Codes in Batch

0 - Black
1 - Blue
2 - Green
3 - Aqua
4 - Red
5 - Purple
6 - Yellow
7 - White
8 - Gray
9 - Light Blue
A - Light Green
B - Light Aqua
C - Light Red
D - Light Purple
E - Light Yellow
F - Bright White

Color values are assigned based on the following order.

  1. The registry value of the default color.
  2. The CMD /T command-line switch.
  3. Color setting that was assigned recently when the CMD launched.

Please note that the COLOR command will not work if you put the same color digit for foreground and background.

Examples to Change Color in Batch

Let’s look at an example to make it more clear to us. Suppose we want to colorize our screen with the foreground of Red, whose color code is 4, and the background of Light Yellow, whose color code is E.

Now the command will look like this.

@echo off
COLOR E4
ECHO I am Colored
PAUSE

Please note that the COLOR command will not work if you put the same color digit for foreground and background. Let’s take a look at the below example.

@echo off
COLOR EE
ECHO I am not Colored
PAUSE

Looking at the above code, you will notice that we use the same color digit for foreground and background on this command, COLOR EE.

If we run this code, the COLOR command will make no change on the screen, and the output I am not Colored will color as the default terminal color, COLOR 07.

When you run this small code, you will see on the output that the text I am Colored is Red as it’s the foreground color, and the background is Light Yellow.

Remember all methods discussed here are written using Batch Script and only work in a Windows CMD environment.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - Batch Script