Windows console width in environment variable

I like the approach using the built-in mode command in Windows. Try the following batch-file:

@echo off
for /F "usebackq tokens=2* delims=: " %%W in (`mode con ^| findstr Columns`) do set CONSOLE_WIDTH=%%W
echo Console is %CONSOLE_WIDTH% characters wide

Note that this will return the size of the console buffer, and not the size of the window (which is scrollable).

If you wanted the height of the windows console, you can replace Columns in the findstr expression with Lines. Again, it will return the height of the buffer, not the window... I personally like to have a big buffer to allow scrolling back through history, so for me the Lines usually reports about 3000 :)


Just for fun, here's a version that doesn't use findstr to filter the output... in case (for some reason) you have a dislike of findstr:

@echo off
for /F "usebackq tokens=1,2* delims=: " %%V in (`mode con`) do (
    if .%%V==.Columns (
        set CONSOLE_WIDTH=%%W
        goto done
    )
)
:done
echo Console is %CONSOLE_WIDTH% characters wide

Note, this was all tried in Windows XP SP3, in a number of different windows (including one executing FAR manager).


try this (language/locale/.net independent):

@ECHO OFF
SET "ConsoleWidth="
SET /A LINECOUNT=0
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=1,2,*" %%A IN ('mode con') DO (SET /A LINECOUNT=!LINECOUNT!+1&IF !LINECOUNT! EQU 4 SET ConsoleWidth=%%B)
SETLOCAL DISABLEDELAYEDEXPANSION
SET "LINECOUNT="
ECHO ConsoleWidth: %ConsoleWidth% characters

tested on Windows XP and Windows 7, both in Czech language


Powershell's (Get-Host).UI.RawUI.WindowSize property sets or returns the dimensions of the current console window. You can capture it with a for loop thusly:

for /f %%I in ('powershell ^(Get-Host^).UI.RawUI.WindowSize.width') do set width=%%I