Introduction
One day I got to thinking about how the screen colours could be changed at the DOS command prompt. ANSI, ASCII, and Unicode characters and commands could be called by using different character code pages. I decided to see if things had changed and how it would be done today.
I was surprised how confusing doing this was. Admittedly I probably made this more difficult that in could have been by only using Notepad and the command prompt rather than a programming editor.
ANSI
In the 1970s the American National Standards Institute (ANSI) introduced a set of control codes for terminal displays and keyboards. The control codes codes can control the text and background colours of the screen, the cursor position and even the screen mode which controlled the number of columns seen on the screen. The codes for the keyboard could emulate any key press including the function keys. This meant that scripts could be written to remap keyboard keys or a macro produced to emulate a series of key presses.
The original MS DOS was introduced in August 1981 and did not include ANSI support. MD DOS v2 was released in March 1983, that and all future editions of it, included the ansi.sys device driver that could use the ANSI terminal control codes. Ansi.sys was not loaded by default but could easily be loaded by editing the config.sys file.
No version of Windows natively supported the ANSI codes until the Windows 10 version 1511 update of November 2015 and is enabled by default.
Using the ANSI Codes
Code Pages
A coe page is is a table or list of character codes and their corresponding glyphs or characters. Microsoft Learn lists the 152 code pages available on Windows systems, but there are just a few that I am interested in; 437 which is the default code page for North America, and 65001 which is the UTF-8 Unicode code page. UTF-8
CHCP
The CHCP command gives or sets the current code page. CHCP on its own will return the current code page, CHCP followwed by a valid code page ID will change the code page to that ID. If the ID does not match a valid code page then the message "Invalid code page" is displayed.
The following batch file will save the current code page ID to a variable, it will then set the code page to 65001, which is the UTF-8 code page
@echo off set newcpage=65001 For /f "tokens=2 delims=:" %%g in ('chcp') Do set oldcpage=%%g echo. echo The current code page ID is%oldcpage% and has been saved chcp %newcpage% > null echo The code page has now been set to code page ID %newcpage% chcp %oldcpage% > null echo The code page has now been reset to code page ID%oldcpage% echo.
The lines starting with chcp and ending with > null simple redirects the output of the command to the null device which suppresses the text output.
Box Drawing Characters
Unicode
Windows Terminal
Windows terminal was introduced on May 3, 2019 and Windows 11 users received it with the 22H2 update when it replaced Windows Console. Terminal is a command-line front-end. It can run multiple command-line apps, including text-based shells in a multi-tabbed window. It has out-of-the-box support for Command Prompt, PowerShell, and Bash on Windows Subsystem for Linux (WSL). It can also natively connect to Azure Cloud Shell. Terminal can also run on Windows 10 and is available from the Microsoft Store.
Of interest to this page is that various colour schemes can be set, either collectively or for individual shells.
Of course these only apply to the interface not to scripts created with them
Adjusting the Console colours in Windows 10
The Command and PowerShell prompts share the same interface but are adjusted individually
Color Command
The COLOR command has been around since DOS and not a command I used very often. One problem with it is that it is not persistant, the colors given to the command are for the current session only. Another is that it changes the entire console window, there is no way for it to control individual lines of text. One good thing about it is that does not allow you to choose the same color for the background and text.
It works by giving a hexidecimal value for the text or foreground and optionally one for the background. The avilable colours and ther codes are:
Color command colours and codes
The following batch file goes through the background colors, except 0 (black), setting the foreground to 0 (black) for each one.
@ECHO OFF SETLOCAL enabledelayedexpansion REM Using a FOR loop to step through a hex string ECHO. FOR %%G IN (1 2 3 4 5 6 7 8 9 A B C D E F) DO ( ECHO. Background colour is %%G COLOR %%G0 PAUSE ) REM Reset the colors COLOR
Sources and Resources
ANSI escape code (Wikipedia)
ANSI.SYS (Wikipedia)
Code page 437 (Wikipedia)
Code Page Identifiers (Microsoft Learn)
Color (Microsoft Learn)
Color (SS64)
Color command (Computer Hope)
Color schemes in Windows Terminal (Microsoft Learn)
Install and get started setting up Windows Terminal (Microsoft Learn)
MS-DOS (Wikipedia)
MS-DOS and Windows command line ansi.sys command (Computer Hope)
Rob van der Woude's Scripting Pages - A wonderful resource for all sorts of scripts
Timeline of DOS operating systems (Wikipedia)
UTF-8 (Wikipedia)
Windows code page (Wikipedia)
Windows Console (Wikipedia)
Windows Terminal (Wikipedia)