I know with linux when I want to change the color code I can type in

echo -e \033[COLORm]

Is there an easy way like this to change the color in windows scripting. I tried

@echo off
REM This program will list all the colors 
REM Selected by user input

set /p color=Enter the color you would like to use to display the sentence Hello World: 

if %color% == [Blue]
color 01 
Echo "Hello World"
Pause
Exit 0

Why can't I simply type in color followed by the code

But I had no luck, I'm still new at this. Any suggestions where I could just type in echo followed by color then code like linux.

Dani AI

Generated

Windows 7 cmd.exe does not interpret ANSI escape sequences (like Linux \033[...m) in echo. The built-in way to change colors in a batch script is the color command, but it changes the console’s foreground/background for subsequent output, not inline within a single echo. That is why echo cannot be followed by an escape code on Win7. See Microsoft’s docs for details on color and its two-hex-digit syntax [bg][fg] (COLOR command).

If you only need to color a specific line, change the color, print, then restore the default:

@echo off
color 0A
echo Hello World
color

color 0A sets black background with light green text; color with no arguments restores the console’s startup colors.

If you need per-word coloring similar to ANSI, consider invoking PowerShell from your batch file:

powershell -NoProfile -Command ^
  "Write-Host 'Hello ' -NoNewline -ForegroundColor Yellow; Write-Host 'World' -ForegroundColor White"

On newer Windows 10+ consoles you can enable Virtual Terminal processing to use ANSI sequences, but that capability does not exist in Windows 7’s console host. For background, see Microsoft’s notes on Console Virtual Terminal Sequences.

Recommended Answers

All 3 Replies

shaniaslam windows 7

  1. Bold Text Here



        • Item One*************



        • Item Two



        • Item Three
  • *

  • *************

You can, the syntax of the color command is COLOR [background][foreground]. From there you use the following table

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



So if you want yellow text on a blue background you type
  COLOR 16

Hope this helps.
commented: This is correct answer. Although not accepted. It is. +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.