How to Control the Command Prompt Window Size in a Batch Script
By default, a batch script runs in a command prompt window with a standard, often small, size. For scripts that display formatted text, user menus, or large amounts of data, controlling the window's dimensions (its columns for width and lines for height) is essential for creating a professional and readable user experience. The built-in MODE
command is the primary tool for this task.
This guide will provide a comprehensive explanation of how to use the MODE CON
command to set your batch script's window size, clarify the critical relationship between the window size and the screen buffer, and offer practical, copy-and-paste examples for different scenarios like creating fixed-size dialogs or maximizing the window.
Why Control the Window Size in a Batch Script?
Adjusting the console window size allows you to:
- Improve Readability: Prevent long lines of text from wrapping awkwardly, making log files or data tables easier to read.
- Create Better User Interfaces: Design text-based menus and dialogs that have a consistent, clean appearance.
- Ensure Proper Formatting: Guarantee that ASCII art or formatted text displays correctly without being broken by window edges.
The Core Command: MODE CON
The MODE
command is a versatile utility for configuring various system devices. To target the console window, you use the CON
device name.
Syntax
MODE CON: [COLS=c] [LINES=l]
COLS=c
: Sets the number of columns (characters) a line can have. This controls the width of the window.LINES=l
: Sets the number of lines visible in the window at one time. This controls the height of the window.
Simple Script Example
This script sets the window to be 90 characters wide and 10 lines tall.
@ECHO OFF
CLS
ECHO Setting window to 90 columns by 10 lines...
ECHO(
:: -- Set the window size --
MODE CON: COLS=90 LINES=10
ECHO The window size has been changed.
ECHO This line should be visible, and any longer lines will wrap after 90 characters.
ECHO(
PAUSE
Output:
The window size has been changed.
This line should be visible, and any longer lines will wrap after 90 characters.
Press any key to continue . . .
The Crucial Concept: Window Size vs. Screen Buffer Size
A common source of confusion and errors when using MODE CON
is the difference between the window size and the screen buffer size.
- Window Size: The physical dimensions of the window you see on the screen (the "viewport").
- Screen Buffer Size: The "virtual" size of the console's memory for text. It determines how many lines of text are kept in memory and how far you can scroll back up.
The Golden Rule of Sizing
The window size can never be larger than the buffer size. If you try to set LINES=50
but the screen buffer is only 25 lines high, the command will fail, often with an "Invalid function" error message.
How to Correctly Set a Large Window Size
To set a large window size, you must first ensure the buffer is even larger. You can set the buffer size using the exact same MODE
command. The command processor is smart enough to know that if you set LINES
to a large value, you mean to set the buffer height.
Example: Creating a Large Window
This script correctly creates a large window (120 columns x 40 rows) by first setting the buffer height to a much larger value.
@ECHO OFF
CLS
ECHO Creating a large console window (120x40).
ECHO(
REM First, set a large buffer to accommodate the new window size.
REM We set the width (COLS) and a very large buffer height (LINES).
MODE CON: COLS=120 LINES=1000
REM Now that the buffer is large enough, set the desired visible window size.
REM The width is already 120, so we only need to set the visible lines.
MODE CON: COLS=120 LINES=40
ECHO This window is now 120 characters wide and 40 lines tall.
ECHO This is useful for displaying wide log files or data tables without wrapping.
ECHO(
PAUSE
Output:
This window is now 120 characters wide and 40 lines tall.
This is useful for displaying wide log files or data tables without wrapping.
Press any key to continue . . .
This two-step process (set buffer, then set window) is the reliable way to create larger-than-default console windows.
Practical Use Cases and Examples
Creating a Small, Fixed-Size "Menu Box"
This is perfect for menus or simple prompts where you want a compact, controlled interface.
@ECHO OFF
TITLE Main Menu
MODE CON: COLS=50 LINES=15
:MENU
CLS
ECHO ==================================================
ECHO APPLICATION MENU
ECHO ==================================================
ECHO(
ECHO 1. Run Backup
ECHO 2. View Log File
ECHO 3. Exit
ECHO(
SET /P "choice=Enter your choice [1,2,3]: "
IF "%choice%"=="3" GOTO :EOF
REM (Add logic for other choices here)
GOTO MENU
Output (with user input 3
)
==================================================
APPLICATION MENU
==================================================
1. Run Backup
2. View Log File
3. Exit
Enter your choice [1,2,3]: 3
Maximizing the Window to Fill the Screen (Approximation using wmic
)
While there is no single command to "maximize" a window like clicking the maximize button, you can use wmic
to get the current screen resolution and then use MODE
to set the window to a very large size. The OS will automatically cap it at the maximum possible size for your display.
@ECHO OFF
CLS
ECHO Attempting to set the console window to maximum size...
REM Get the screen dimensions using WMIC
FOR /F "tokens=*" %%A IN ('wmic path Win32_VideoController get CurrentHorizontalResolution^,CurrentVerticalResolution /VALUE ^| find "="') DO (
SET %%A
)
REM Calculate approximate character columns and lines based on standard font sizes
REM (8 pixels wide, 16 pixels high is a rough estimate)
SET /A "max_cols=%CurrentHorizontalResolution% / 8"
SET /A "max_lines=%CurrentVerticalResolution% / 16"
REM Set the buffer and window size to the calculated maximums
REM Setting a very large line buffer (e.g., 9999) is common practice.
MODE CON: COLS=%max_cols% LINES=9999
REM Note: The OS will adjust these values down to the maximum supported by the display.
ECHO Window has been set to the approximate maximum size for this display.
PAUSE
Output:
Window has been set to the approximate maximum size for this display.
Press any key to continue . . .
Important Considerations
- Temporary Change: The
MODE
command only affects the current console session. It does not permanently change your default Command Prompt settings. - Physical Limits: The window size is ultimately limited by your monitor's resolution and Windows' own constraints.
- Minimum Size: Windows has a minimum allowed size for a console window, so setting
COLS
orLINES
to extremely small values (e.g., less than ~25x6) may not work as expected or may be overridden by the system.
Conclusion
The MODE CON: COLS=c LINES=l
command is the essential built-in tool for programmatically controlling the size of the command prompt window from within a batch script.
By understanding the critical relationship between the window size and the screen buffer size—and setting the buffer first when creating large windows—you can create clean, readable, and professional-looking user interfaces for your scripts.
Whether for fixed-size menus or for maximizing the display area for data output, mastering the MODE
command gives you a new level of control over your script's presentation.