- Updated based in comments from
@phuclv
@echo off && setlocal EnableDelayedExpansion
set "_color="0 Black","1 DarkBlue","2 DarkGreen","3 DarkCyan","4 DarkRed","
@set "_color=!_color!"5 DarkMagenta","6 DarkYellow","7 Gray","8 DarkGray","
set "_color=!_color!"9 Blue","A Green","B Cyan","C Red","D Magenta","
set "_color=!_color!"E Yellow","F White"" && cd/d "%~dp0" && title %0
;for /f %%I in ('powershell echo "$([console]::ForegroundColor) $([console]::BackgroundColor)"
')do for %%# in (!_color!)do set "_Hex=%%~#"&& for /f %%a in ('cd')do if "%%~I"=="!_Hex:~2!" (
if not "!_FB!"=="!_Hex:~1,1!" ( set "_FB=!_Hex:~0,1!!_FB!" && set "_L= !_Hex:~2!!_L!"))
set "_L=!_L:~1!"&& cmd/v/c echo The color is currently set to !_FB! (!_L: =/!^)&&endlocal
The color is currently set to 0A (Black/Green)
rem :: powershell command ::
echo "$([console]::BackgroundColor) $([console]::ForegroundColor)"
@echo off && setlocal EnableDelayedExpansion
set "_color="0 Black","1 DarkBlue","2 DarkGreen","3 DarkCyan","4 DarkRed","
;set "_color=!_color!"5 DarkMagenta","6 DarkYellow","7 Gray","8 DarkGray","
set "_color=!_color!"9 Blue","A Green","B Cyan","C Red","D Magenta","
set "_color=!_color!"E Yellow","F White"" && cd/d "%~dp0" && title %0
for /f %%I in ('powershell echo "$Host.UI.RawUI.BackgroundColor $Host.UI.RawUI.ForegroundColor"
')do for %%# in (!_color!)do set "_Hex=%%~#"&& for /f %%a in ('cd')do if "%%~I"=="!_Hex:~2!" (
if not "!_FB!"=="!_Hex:~1,1!" ( set "_FB=!_Hex:~0,1!!_FB!" && set "_L= !_Hex:~2!!_L!" ))
set "_L=!_L:~1!"&& cmd/v/c echo The color is currently set to !_FB! (!_L: =/!^)&&endlocal
The color is currently set to 0A (Black/Green)
rem :: powershell command ::
echo "$Host.UI.RawUI.BackgroundColor $Host.UI.RawUI.ForegroundColor"

- Generate C# .exe in Run Time
This bat file that generate a file cFB.cs (colorForegroundBackground.C#) and in run time, will build the executable cFB.exe for execute them using ConsoleColor Enum
@echo off && setlocal EnableDelayedExpansion
set "_color="0 Black","1 DarkBlue","2 DarkGreen","3 DarkCyan","4 DarkRed","
@set "_color=!_color!"5 DarkMagenta","6 DarkYellow","7 Gray","8 DarkGray","
set "_color=!_color!"9 Blue","A Green","B Cyan","C Red","D Magenta","
set "_color=!_color!"E Yellow","F White"" && cd/d "%~dp0" && title %0
for %%D in (.exe,.cs) do if exist "%temp%\cFB%%~D" (2>nul >nul del /q /f "%temp%\cFB%%~D")
set "_csc=%windir%\Microsoft.NET"&& set "_where=%__appdir__%where.exe" && set "_cs=cFB.cs"
set "_arg=/t:exe /out:"%tmp%\!_cs:~,-3!.exe" "%tmp%\!_cs!" /platform:x86 /unsafe+ /w:0 /o"
set "_c=!_where! /r "!_csc!" "csc.exe" "&& set "_#=%temp%\!_cs!" && cmd/v/c echo=&>"!_#!"^
(
echo/ using System; namespace cFB ^{class Program ^{public static void Main(^)^{
echo/ ConsoleColor currentForeground=Console.ForegroundColor;
echo/ ConsoleColor currentBackground=Console.BackgroundColor;
echo/ Console.WriteLine("{0}\n{1}",Console.ForegroundColor,Console.BackgroundColor^);^}^}^}
) && (pushd "%temp%" & goto :run)||echo=Well, something is really wrong here^!! & goto :Err
:run
for /f tokens^=* %%i in ('!_c!^|find "k\v2"')do "%%~i" /nologo !_arg!&& if exist "!_#:~0,-3!.exe" (
for /f ^delims^=^ ^eol^= %%r in ('"!_#:~0,-3!.exe"')do set "_Hex=%%r") else (popd && cls 2>nul && (
echo=File: "!_#:~0,-3!.exe" not found, something is really wrong here^^!!& timeout -1& goto :Err) )
for /f tokens^=^*^delims^= %%I in ('"!_#:~0,-3!.exe"')do for %%# in (!_color!)do set "_h=%%~#" && (
if "%%~I"=="!_h:~2!" if not "!_fb!"=="!_h:~0,l!" (set "_fb=!_h:~0,1!!_fb!"&&set "_l= !_h:~2!!_l!"))
set "_l=!_l:~1!" && for %%D in (.exe,.cs) do if exist "%temp%\cFB%%~D" >nul del /q "%temp%\cFB%%~D"
cmd /v /c echo The color is currently set to: !_fb! (!_l: =/!^) && endlocal && exit /b || goto :EOF
:Err
endlocal & exit /b || goto :EOF
This is C# code without escaping:
using System;
namespace CBF
{
class Program
{
public static void Main()
{
ConsoleColor currentBackground=Console.BackgroundColor;
ConsoleColor currentForeground=Console.ForegroundColor;
Console.WriteLine("{0}\n{1}",Console.ForegroundColor,Console.BackgroundColor);
}
}
}
- Bat Run C# Commmand Output:
The color is currently set to: 0A (Black/Green)
- Command line to build executable:
"C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe" /nologo /t:exe /out:"%temp%\cFB.exe" "%temp%\cFB.cs" /platform:x86 /w:0 /o
You also, can try get some help with $Host.UI.RawUI in powershell?
@echo off && setlocal EnableDelayedExpansion
set "_color="0 Black","1 DarkBlue","2 DarkGreen","3 DarkCyan","4 DarkRed","
@set "_color=!_color!"5 DarkMagenta","6 DarkYellow","7 Gray","8 DarkGray","
set "_color=!_color!"9 Blue","A Green","B Cyan","C Red","D Magenta","
set "_color=!_color!"E Yellow","F White""&& cd/d "%~dp0" && title %~0
for /f tokens^=2^delims^=^:^ %%I in ('powershell -nOp $Host.UI.RawUI^|find "Color"')do (
for %%# in (!_color!)do set "_Hex=%%~#"&& for /f %%a in ('cd')do if "%%~I"=="!_Hex:~2!" (
if not "!_FB!"=="!_Hex:~1,1!" ( set "_FB=!_Hex:~0,1!!_FB!" && set "_L= !_Hex:~2!!_L!" )))
set "_L=!_L:~1!"&& cmd/v/c echo The color is currently set to !_FB! (!_L: =/!^)&&endlocal
The color is currently set to A3 (Green/DarkCyan)
rem :: just type ::
powershell -nop -c "$Host.UI.RawUI"|find "Color"
rem :: or -nop -c
powershell "$Host.UI.RawUI"|find "Color"
ForegroundColor : White
BackgroundColor : Blue
- $Host.UI.RawUI Full output:
ForegroundColor : White
BackgroundColor : Blue
CursorPosition : 0,108
WindowPosition : 0,90
CursorSize : 25
BufferSize : 99,770
WindowSize : 89,48
MaxWindowSize : 99,50
MaxPhysicalWindowSize : 174,50
KeyAvailable : False
WindowTitle : Q59449889v2.cmd - powershell $Host.UI.RawUI
Sorry my limited English