I love a nice command prompt window. I typically customize the CMD shortcut so that the window size is set the way I like it. However, this can also be done on the fly and even from a batch file. Should you need to resize the command window, you can do this with the MODE command.
If you type MODE con: you will see the current settings for the command window. To change the settings run:
mode con: cols=X lines=Y
Where X and Y are integers. You can set either or both. If you run mode con: cols=160, your command window will double in size (assuming you are using the default 80 column width). Given today’s larger screens, a command window of this size is not unreasonable.
Let’s say your batch file is going to generate some pretty wide output, say from a DSQUERY command and you don’t want the output to wrap. You can easily adjust the screen width. But what about setting it back when you are finished? Easy, use commands like this to capture the current settings to environmental variables.
E:\>for /f “tokens=2 delims=: ” %c in (‘mode con: ^|find /i “columns”‘) do @set CurrentCol=%c
E:\>for /f “tokens=2 delims=: ” %l in (‘mode con: ^|find /i “lines”‘) do @set CurrentLines=%l
After your script is finished, run MODE again to change the values back. Here’s a sample:
@echo off
::ModeDemo.bat
REM Capture current console settings
for /f “tokens=2 delims=: ” %%c in (‘mode con: ^|find /i “columns”‘) do @set CurrentCol=%%c
for /f “tokens=2 delims=: ” %%l in (‘mode con: ^|find /i “lines”‘) do @set CurrentLines=%%l
REM Resize Window
mode con: cols=160
REM Find all disabled users
dsquery user dc=Mydomain,dc=com -o upn -disabled
pause
REM Reset Window
mode con: cols=%CurrentCol%
REM Clear Variables
set CurrentCol=
set CurrentLines=
:EOF
There is one “gotcha” that may not affect you. I set my command windows with a buffer setting of 2500. This is can be set when you modify the properties of your command window. This works just fine because the command window will use a scroll bar. However, even though my window size in the properties is set to 25, MODE shows a line property of 2500. So what’s the big deal? When I run this demo script, the command window widens appropriately, but when it switches back it resizes the window to try and show all 2500 lines without a scroll bar. What this means for you is to properly test any script that might use this command, especially if your script will be running on any system that has a customized command window.