In Windows CMD I can type mode con /status
to see the current line- and column display limits of the CMD. Is it possible, in a similar manner, to figure out how many lines and columns can currently fit inside the CMD window? mode
displays the amount of lines and columns that fits inside the buffer, but I need the amount that fits visibly inside the CMD window. In other words, I'm looking for a Windows CMD equivalent for the Unix commands tput cols
and tput lines
.
4 Answers
PowerShell gives ready access to a number of console properties:
C:\>powershell -command "&{$H=get-host;$H.ui.rawui;}"
ForegroundColor : Gray
BackgroundColor : Black
CursorPosition : 0,179
WindowPosition : 0,131
CursorSize : 25
BufferSize : 2000,3000
WindowSize : 144,49
MaxWindowSize : 200,85
MaxPhysicalWindowSize : 200,85
KeyAvailable : False
WindowTitle : Command Prompt - powershell -command "&{$H=get-host;$H.ui.rawui;}"
I'm sure there is a simple way in powershell to get just the values that you want, but I am a neophyte at PowerShell. So I will use the old command line utilities instead.
C:\>powershell -command "&{$H=get-host;$H.ui.rawui;}"|findstr /b WindowSize
WindowSize : 144,49
If you need the values in a batch script, then a FOR /F can parse the values for you:
@echo off
setlocal
for /f "tokens=3,4 delims=, " %%A in (
'powershell -command "&{$H=get-host;$H.ui.rawui;}"^|findstr /b WindowSize'
) do set /a windowWidth=%%A, windowHeight=%%B
Or you could learn PowerShell and do the entire thing within that much more powerful scripting language.
-
powershell -command "&{$H=get-host;$H.ui.rawui.WindowSize;}"
gives only current window size.– hexcoderFeb 20, 2019 at 12:05 -
Is there a reason why you didn't write
powershell -command (get-host).ui.rawui
? It's neat and easier to remember than$H=get-host;$H.ui.rawui
. Mar 23, 2020 at 3:12
The amount that fits visibly in maximum inside the CMD window can be displayed with:
powershell -command "&{(get-host).ui.rawui.MaxPhysicalWindowSize;}"
The output will be e.g.:
Width Height
----- ------
240 63
If only the amount in the current CMD window should be shown, use:
powershell -command "&{(get-host).ui.rawui.WindowSize;}"
If you just want to see it, you can right click the title bar, go to properties, go to layout, then in "Window Size" it will show it to you.
more.com
couldn’t work otherwise. However, since this is a question about the Windows API, I vote for moving it to SO.mode con /status | sls -Pattern "Lines:[ ]+"