4

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.

3
  • 1
    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, 2013 at 10:58
  • 4
    Even better, it's being asked in English, so I'm voting for migration to English Language & Usage! Nov 26, 2013 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, 2013 at 11:56

4 Answers 4

5

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.

2
  • powershell -command "&{$H=get-host;$H.ui.rawui.WindowSize;}" gives only current window size.
    – hexcoder
    Feb 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.
    – zypA13510
    Mar 23, 2020 at 3:12
1

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;}"
1

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.

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .