3

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.

  • It is possible — more.com couldn’t work otherwise. However, since this is a question about the Windows API, I vote for moving it to SO. – kinokijuf Nov 26 '13 at 10:58
  • 4
    Even better, it's being asked in English, so I'm voting for migration to English Language & Usage! – That Brazilian Guy Nov 26 '13 at 11:40
  • You could pipe in Powershell to get you close, but I'm not good enough at PS to get you all the way there: mode con /status | sls -Pattern "Lines:[ ]+" – LuckySpoon Nov 26 '13 at 11:56
4

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.

| improve this answer | |
  • powershell -command "&{$H=get-host;$H.ui.rawui.WindowSize;}" gives only current window size. – hexcoder Feb 20 '19 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. – zypA13510 Mar 23 at 3:12
0

Searching for a C++ program that does this and I've found this

And so because I needed this as well, I added some modifications to it and compiled it.

You can download it from here, includes pre-compiled binaries (both architectures), hashes and source code.

| improve this answer | |
0

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;}"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.