How can I get the current width of the windows console in an environment variable within a batch file?
6 Answers
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).

- 6,992
- 25
- 41
-
2This approach crashes if the system is using any other language than english. Look at @user2475020 's answer if you want language independency. – SapuSeven Jul 14 '17 at 08:35
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

- 51
- 1
- 1
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

- 24,000
- 5
- 55
- 101
-
This assumes powershell is installed though, which might not be the case for XP users. – Axel Fontaine Feb 20 '13 at 13:10
-
1+1, The powershell command can be as simple as `powershell (get-host).ui.rawui.windowsize.width`. Background info is available at http://blogs.technet.com/b/heyscriptingguy/archive/2006/12/04/how-can-i-expand-the-width-of-the-windows-powershell-console.aspx – dbenham Feb 20 '13 at 13:13
-
@AxelFontaine - I don't think there is a pure batch solution. The info is exposed via .NET, and as far as I know, Powershell is the only native scripting language that can access .NET. Unfortunately, you are correct, Powershell may not be available on an XP machine. – dbenham Feb 20 '13 at 13:26
-
Here is a better reference from Microsoft: http://technet.microsoft.com/en-us/library/ee156814.aspx. Buried within it states how PowerShell gains access to the properties via .NET – dbenham Feb 20 '13 at 13:31
-
@AxelFontaine - challenge: accepted. See my [other answer](http://stackoverflow.com/a/14981186/1683264). – rojo Feb 20 '13 at 13:35
Alright, here's one that doesn't require powershell to be installed. It composes, runs and deletes a .Net application to set a batch script variable. :)
@echo off
setlocal
pushd "%windir%\microsoft.net\"
for /f "delims=" %%I in ('dir /s /b csc.exe') do (
set csc=%%I
goto next
)
:next
popd
echo using System;>width.cs
echo class Width {>>width.cs
echo public static void Main() {>>width.cs
echo string m1 = "{0}";>>width.cs
echo Console.WriteLine^(m1, Console.WindowWidth^); } }>>width.cs
"%csc%" /out:width.exe width.cs >NUL 2>NUL
for /f %%I in ('width.exe') do set width=%%I
del width.exe width.cs
echo %width%

- 24,000
- 5
- 55
- 101
-
This answer relies on having .Net installed, and doesn't check if `csc.exe` doesn't exist. That said, I approve of the extreme coding! Well done. – icabod Feb 20 '13 at 13:57
-
This works...but it's a little confusing because you used `.vb` for the source file extension, but your code is actually C#. – aphoria Feb 20 '13 at 15:40
-
Ya, I started out writing it in vb but switched to c#. I didn't feel like editing the file extensions but I'll go ahead and do it since someone noticed. :) – rojo Feb 20 '13 at 15:45
You can't get it in an environment variable, but it is stored in the registry so you can access it from your batch script.
There are answers here about how to change it: How to change Screen buffer size in Windows Command Prompt from batch script
In a similar way you can use reg.exe QUERY [key details]
rather than reg.exe ADD [details]
. See the Technet documentation for HKCU\Console
for details.
-
How does this work with multiple consoles and multiple widths? How can I know which key applies to my current console (as opposed to simply the default width for a new console)? – Axel Fontaine Feb 20 '13 at 11:38
-
When I get to work I'll link to the technet article for `hkcu\console\windowsize`. Also, I know there is a powershell-ish way of setting the current window dimensions and buffer size. I'll see if I can find something similar to read the dimensions rather than set. – rojo Feb 20 '13 at 11:43
-
@rojo: there's already an answer from you on the question I linked to using a powershell method. – Vicky Feb 20 '13 at 12:24
For a one simple line:
for /f tokens^=2 %%w in ('mode con^|find "Col"')do set _width=%%~w"

- 2,514
- 3
- 22
- 29