CONSOLE.BAT - Get and set console window attributes via hybrid batch/powershell
Posted: 30 Mar 2016 20:20
This first post contains an outdated version. The current version of CONSOLE.BAT is available here.
Batch has limited ability to get or set console attributes, but PowerShell has extensive control. This hybrid Batch/PowerShell utility brings much of the PowerShell capability into the batch world using pure script - no compiled binaries are required.
One negative aspect of this utility is it is relatively slow.
The only other negative is PowerShell is not standard for XP. But XP is no longer supported by MicroSoft, so this should become less an less an issue over time.
Some of the nice features:
- Set Screen size independent of Buffer size
- Set color attributes of future output without changing color of existing screen content
- Reposition the cursor anywhere within the current buffer area.
Dave Benham
Batch has limited ability to get or set console attributes, but PowerShell has extensive control. This hybrid Batch/PowerShell utility brings much of the PowerShell capability into the batch world using pure script - no compiled binaries are required.
One negative aspect of this utility is it is relatively slow.
The only other negative is PowerShell is not standard for XP. But XP is no longer supported by MicroSoft, so this should become less an less an issue over time.
Some of the nice features:
- Set Screen size independent of Buffer size
- Set color attributes of future output without changing color of existing screen content
- Reposition the cursor anywhere within the current buffer area.
Code: Select all
@echo off
:::console.bat [option=value|/Switch]...
:::
::: Utility for getting and/or setting various console window attributes.
:::
::: Displays this help if no options are specified.
:::
::: Options: Used to set console attribute values. Options are always
::: processed in the order in which they appear.
:::
::: WX={WindowWidthValue}
::: WY={WindowHeightValue}
::: BX={BufferWidthValue}
::: BY={BufferHeightValue}
::: CX={CursorPositionXCoordinate}
::: CY={CursorPositionYCoordinate}
::: CS={CursorSizeValue}
::: BC={BackgroundColorValue}
::: FC={ForegroundColorValue}
:::
::: Switches: Always processed after all options, regardless of order.
:::
::: /G - Get various console attributes and store the values in the
::: following environment variables:
:::
::: console.BufferSize.Height
::: console.BufferSize.Width
::: console.ColorBackground.Name
::: console.ColorBackground.Num
::: console.ColorForeground.Name
::: console.ColorForeground.Num
::: console.CursorPosition.X
::: console.CursorPosition.Y
::: console.CursorSize
::: console.WindowMaxPhysicalSize.Height
::: console.WindowMaxPhysicalSize.Width
::: console.WindowMaxSize.Height
::: console.WindowMaxSize.Width
::: console.WindowPosition.X
::: console.WindowPosition.Y
::: console.WindowSize.Height
::: console.WindowSize.Width
:::
::: /D - Get the same console attributes described for /G but write the
::: values to stdout instead of storing them in environment variables.
:::
::: /D and /G can be combined to both store the values in environment
::: variables and write the values to stdout.
:::
::: console.bat version 1.0 was written by Dave Benham
:::
setlocal enableDelayedExpansion
for %%v in (att obj flag) do for /f "delims==" %%V in ('set %%v_ 2^>nul') do set "%%V="
set "obj_wx=WindowSize"
set "obj_wy=WindowSize"
set "obj_bx=BufferSize"
set "obj_by=BufferSize"
set "obj_cx=CursorPosition"
set "obj_cy=CursorPosition"
set "obj_cs=CursorSize"
set "obj_bc=BackgroundColor"
set "obj_fc=ForegroundColor"
set "att_wx=Width"
set "att_wy=Height"
set "att_bx=Width"
set "att_by=Height"
set "att_cx=X"
set "att_cy=Y"
set "cmd="
set "get="
set "var="
set "show="
set /a Black=0, DarkBlue=1, DarkGreen=2, DarkCyan=3, DarkRed=4, DarkMagenta=5, DarkYellow=6, Gray=7, DarkGray=8, Blue=9, Green=10, Cyan=11, Red=12, Magenta=13, Yellow=14, White=15
:parse
if "%~1"=="" goto :go
if not defined cmd set "cmd=$U=$Host.ui.rawui;"
if /i "%~1"=="/G" (
set /a get=var=1
) else if /i "%~1"=="/D" (
set /a get=show=1
) else if defined att_%~1 (
if not defined flag_!obj_%~1! (
set flag_!obj_%~1!=1
set "cmd=!cmd!$!obj_%~1!=$U.!obj_%~1!;"
)
set "cmd=!cmd!$!obj_%~1!.!att_%~1!=%~2;$U.!obj_%~1!=$!obj_%~1!;"
shift /1
) else (
set "cmd=!cmd!$U.!obj_%~1!=%~2;"
)
shift /1
goto :parse
:go
if defined get set "cmd=!cmd!write-host($U.WindowSize,$U.BufferSize,$U.WindowPosition,$U.MaxWindowSize,$U.MaxPhysicalWindowSize,$U.CursorPosition,$U.CursorSize,$U.BackgroundColor,$U.ForegroundColor);"
if not defined cmd (
for /f "tokens=* delims=:" %%A in ('findstr "^:::" "%~f0"') do echo(%%A
) else for /f "tokens=1-15 delims=, " %%A in (
'powershell -command "&{%cmd%}"'
) do for /f "tokens=1,2" %%P in ("!%%N! !%%O!") do (
if defined var endlocal
set /a console.WindowSize.Width=%%A,^
console.WindowSize.Height=%%B,^
console.BufferSize.Width=%%C,^
console.BufferSize.Height=%%D,^
console.WindowPosition.X=%%E,^
console.WindowPosition.Y=%%F,^
console.WindowMaxSize.Width=%%G,^
console.WindowMaxSize.Height=%%H,^
console.WindowMaxPhysicalSize.Width=%%I,^
console.WindowMaxPhysicalSize.Height=%%J,^
console.CursorPosition.X=%%K,^
console.CursorPosition.Y=%%L,^
console.CursorSize=%%M,^
console.ColorBackground.Num=%%P,^
console.ColorForeground.Num=%%Q
set "console.ColorBackground.Name=%%N"
set "console.ColorForeground.Name=%%O"
if "%show%"=="1" set console
)
Dave Benham