Is it possible to output the current color code on the command prompt using batch programming?

multiple colors in batch file
batch color codes
add color to batch file
how to color one line in batch file
batch file bold text
cmd color codes
cmd color single line
command to change text color in cmd

I am making a tutorial program for a friend of mine using batching programming. I would like to know if it is possible if there is code I can write in the file that will display the current color code.

Example being the color is currently set to 0A and I want be displayed on the line saying:

echo The color is currently set to 0A.

I want my file to read the code that is set to and display it to help them remember what changes they have made as this is an example program for color codes in the command prompt/batch.

Thank you for your help!

It is easy to make your own command to do this. Copy both below text files into GetConsoleColour.bat and GetConsoleColour.vb in the same folder. Double click the batch file and it will create GetConsoleColour.exe.

PS Colour is spelt right for my culture. As I'm writing it I don't see any need to use American spelling which in programming you usually have to do.

See https://docs.microsoft.com/en-us/windows/console/getconsolescreenbufferinfo

GetConsoleColour.exe prints the current console colour in hex and returns an errorlevel with the value

To use

C:\PathToFile\GetConsoleColour

I have a program here that sets text color line by line. It is the only technique that will work on all Windows computers.

Command Prompt Scripting: Problem with multiple colors in a batch file.

Also a similar program saying how many processes are in this console window - ListConsole.exe list the processes in the current console and returns an errorlevel saying how many . https://winsourcecode.blogspot.com/2019/05/listconsoleexe-list-processes-in.html


REM GetConsoleColour.bat
REM This file compiles GetConsoleColour.vb to GetConsoleColour.exe
REM GetConsoleColour.exe prints the current console colour and returns an errorlevel with the value
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\GetConsoleColour.exe" "%~dp0\GetConsoleColour.vb" 
pause

Note: There is only 4 lines of code here. The rest is just information the program needs to do those 4 lines. In a big program they would be hidden away in a separate file.

'GetConsoleColour.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module MyApplication 

Public Declare Function GetStdHandle Lib "kernel32" Alias "GetStdHandle" (ByVal nStdHandle As Long) As Long
Public Declare Function SetConsoleTextAttribute Lib "kernel32" Alias "SetConsoleTextAttribute" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
Public Declare Function GetConsoleScreenBufferInfo Lib "kernel32" (ByVal hConsoleOutput As Integer, ByRef lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Integer
Public Const STD_ERROR_HANDLE = -12&
Public Const STD_INPUT_HANDLE = -10&
Public Const STD_OUTPUT_HANDLE = -11&

 <StructLayout(LayoutKind.Sequential)> _
Public Structure COORD
    Public x As Short
    Public y As Short
End Structure

 <StructLayout(LayoutKind.Sequential)> _
Public Structure SMALL_RECT
    Public Left As Short
    Public Top As Short
    Public Right As Short
    Public Bottom As Short
End Structure

 <StructLayout(LayoutKind.Sequential)> _
Public Structure CONSOLE_SCREEN_BUFFER_INFO
    Public dwSize As COORD
    Public dwCursorPosition As COORD
    Public wAttributes As Integer
    Public srWindow As SMALL_RECT
    Public dwMaximumWindowSize As COORD
End Structure 


Sub Main()
    Dim hOut as IntPtr
    Dim Ret as Integer
    Dim CSBI as Console_Screen_Buffer_Info
    hOut  = GetStdHandle(STD_OUTPUT_HANDLE)
    Ret = GetConsoleScreenBufferInfo(hOut, CSBI)
    Console.Writeline(Hex(CSBI.wAttributes))
    Environment.ExitCode = CSBI.wAttributes
End Sub
End Module

Add Colors to Batch Files, cecho is an enhanced ECHO command line utility with color support, inspired by the The last section explains how to embed the cecho utility into a batch file using cecho simply redirects the command arguments to the standard output after Simply call the SetConsoleTextAttribute Win32 API with the correct color code. The COLOR command sets ERRORLEVEL to 1 if an attempt is made to execute the COLOR command with a foreground and background color that are the same. Example: "COLOR fc" produces light red on bright white . Errorlevels. When you try to set the foreground and background colors to the same value, COLOR will terminate with a return code (errrlevel

As already indicated in another answer, you can use powershell from your batch-file to show you the current color sequence:

@(Set/P "=The color is currently set to "<NUL&For /F %%# In ('^""%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoP "$Console=(Get-Host).UI.RawUI;Switch($Console.BackgroundColor,$Console.ForegroundColor){'Black'{'0'}'DarkBlue'{'1'}'DarkGreen'{'2'}'DarkCyan'{'3'}'DarkRed'{'4'}'DarkMagenta'{'5'}'DarkYellow'{'6'}'Gray'{'7'}'DarkGray'{'8'}'Blue'{'9'}'Green'{'A'}'Cyan'{'B'}'Red'{'C'}'Magenta'{'D'}'Yellow'{'E'}'White'{'F'}}" 2^>NUL^"')Do @Set/P=%%#<NUL)&Echo(&Pause

You should also be able to do it from the command-prompt thus:

(Set/P "=The color is currently set to "<NUL&For /F %# In ('^""%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoP "$Console=(Get-Host).UI.RawUI;Switch($Console.BackgroundColor,$Console.ForegroundColor){'Black'{'0'}'DarkBlue'{'1'}'DarkGreen'{'2'}'DarkCyan'{'3'}'DarkRed'{'4'}'DarkMagenta'{'5'}'DarkYellow'{'6'}'Gray'{'7'}'DarkGray'{'8'}'Blue'{'9'}'Green'{'A'}'Cyan'{'B'}'Red'{'C'}'Magenta'{'D'}'Yellow'{'E'}'White'{'F'}}" 2^>NUL^"')Do @Set/P=%#<NUL)&Echo(

Batch files - NT's COLOR command, Sets the default console foreground and background colors. COLOR [attr]. attr, Specifies color attribute of console output This value either comes from the current console window, the /T command line switch or from the colors to the same value, COLOR will terminate with a return code (errrlevel) of 1. Although it's not really necessary to do something like this, it's possible. There's a pre-made C++ script compiled into something Windows can run at http://www.codeproject.com/Articles/17033/Add-Colors-to-Batch-Files. The site explains mostly everything you need to know, but I'll give some help an examples here.

By making a compromise and using setlocal EnableDelayedExpansion, you can do so using the following:

@ECHO OFF

Setlocal enableDelayedExpansion

Set "Color=Color 02" && !color!

ECHO Color is currently %color%

pause

The downside of this approach being changing console Color becomes a 2 Step process (depending on how your displaying information).

Color - Foreground and Background colours - Windows CMD, COLOR. Sets the default console foreground and background colours. The current colour settings when cmd was launched The COLOR command will change the color of all the text in the window. PowerShell: Write-Host - Write output to the screen (colour can be set for individual strings). Colour codes - HTML/CSS. Greetings, and thank you for posting your question on Quora. The color command is not batch file specific and is universal to the console’s output. Meaning if set the color.

Change font size in batch file, Command line interpreter takes the file as an input and executes in the same order. Jan 26, 2013 � Is it possible to have an outlook hyperlink that starts a batch file Please help me to change the font size and print the notepad from dos command Notice that you should reset the ANSI color codes after each instance you� Now the Windows Command Prompt will show you the color attributes. Change Command Prompt Color In Windows 10. Step 4. The color attributes are specified by two hex digits – The first one is for background, the second is for the foreground. For example, you can type in Color 16. It will change the background color to Blue and text color to

Windows Programming/Programming CMD, one is able to write batch files that are interpreted by the Command Prompt The batch files can be considered to be a simple scripting language with logic and exit is needed for any remaining output to be read before the console window closes. Color settings, however, are retained on later editions of Windows NT. In� This value either comes from the current console window, the /T command line switch or from the "DefaultColor" registry value. The COLOR command sets ERRORLEVEL to 1 if an attempt is made to execute the COLOR command with a foreground and background color that are the same. Color examples Color 0A

How to create and run a batch file on Windows 10, The above script outputs the phrase, "Congratulations! To run a batch file from Command Prompt, use these steps. Quick tip: If you need to delete as many files as possible, use the 1 day option in the Staying current with Windows 10 allows you to access the latest features and enhancements. Also� Changes the background (first value) and text color (second value) of the command prompt. The color lies between 0 (black) and F (white). 10/8/7/Vista/XP command Starts CMD.COM. 32-bit/DOS date Displays the current date and allows you to change it.

Command-line interface, A command-line interface (CLI) processes commands to a computer program in the form of Using >> will redirect the output and append it to the file. including printing a command 'your administrator has disabled running batch files ' the prompt commonly ends in $ or % if the user is a normal user, but in # if the user is a� Once you've completed the steps, restart Command Prompt to start using the console with the new color scheme. At any time, if you want to go back to the new default color settings, you can apply

Comments
  • Good question, but I am not aware of any native method to do this.
  • Though it does work in single use, It's very hit and miss when it's called upon repeatedly in a function. Often it will only return the first component of the code in effect
  • Whilst I'm unable to test it, so cannot confirm or deny, I really do find it difficult to believe that anyone would need it more than once, @T3RROR. Why would anyone need it in a function? single use should be it's only use! If I do find time over the seasonal break, I'll take a look at it and see if there's any improvement to be made. You can always enter color with no parameters, to return the color to its default, and once you've changed it, you should know what the color is.
  • The reason I tested it as a function is the use the Op described as an example program for a beginner to familiarise themselves with colour codes, indicating the user will be making changes. I was actually hoping it worked, as it seemed an Elegant solution. It struggles most with numerical codes.
  • @T3RR0R, when a user makes a change, they can clearly see the colours in front of them, (there's only 16 of them after all). If someone wants to familiarise themselves, they just need to read the help information for the color command. I honestly don't see a need for this at any time, which probably explains why there's no built-in command in the first place. I was unaware that the strings had been modified by my predictive text, and have corrected them to remove the invalid spaces. Has that fixed the unnecessary function you tested it against?
  • Does appear to be working Flawlessly now. I'm in complete agreeance regarding it being something there's no particular need for, However the Solution you've presented is quite Elegant for something that Batch alone lacks the native capacity to do. I'm a hobby coder. I enjoy the art of problem solving, and like to Formulate, or discover solutions, or to see the multitude of ways others find solutions to problems, regardless of how trivial they may seem.
  • This can also be done with Set /p to Set the Color, However you then need to validate that the user input is a Valid Color Code.
  • The question is how to determine the current color, I'm assuming, both fore and background.?, not to set it to a specific combination! More like 'Your current color combination is 07. Would you like to change it?'
  • As dbenam has said, there's no native way to do so. Had Color information been Included in Mode /Status, A for loop on the output of Mode /Status could have done the Job. When a true solution Doesn't exist, you have to make compromises.
  • Also, The Answer I provided may not Do the action the Question refers to, however it Does achieve the Outcome the question asks for. The Question (Headline, and first component in Body) Asks If it's possible to output the current Colorcode. This answer Outputs the current Color Code. Whilst it does require a Modification in how the Color Code is Applied, It's a small inconvenience given it achieves the goal - Especially when other options are Lacking.
  • instead of writing a C# program, compile and run you can simply run [console]::BackgroundColor and [console]::ForegroundColor directly in PowerShell to get those properties. Or combine them in a single command like echo "$([console]::BackgroundColor) $([console]::ForegroundColor)". Remember you can call any .NET framework methods in PowerShell
  • and instead of powershell -nop -c "$Host.UI.RawUI"|find "Color" just use $Host.UI.RawUI.ForegroundColor and $Host.UI.RawUI.BackgroundColor directly