Question

I can use $host.UI.RawUI.MaxPhysicalWindowSize.Width to get the maximum width (i.e. number of columns) for the PowerShell command window, and setting the size of the PowerShell command window is well documented, but the minimum buffer width seems to vary between machines. On one machine it was 13 but on another it was 14. I can set the minimum window height and width to 1 and the minimum buffer height can also be 1.

Does anyone know how I can obtain this minimum buffer width value programatically (without just trying values and catching the exceptions!)

Was it helpful?

Solution

Because setting $host.UI.RawUI.BufferSize affects the buffer of its console screen, (Command Prompt -> Properties -> Layout -> Screen Buffer Size is modified when you change $host.UI.RawUI.BufferSize), it has the same restrictions on its buffersize as the console screen.

As we can read here, the specified dimensions for the buffersize cannot be less than the minimum size allowed by the system. This minimum depends on the current font size for the console (selected by the user) and the SM_CXMIN and SM_CYMIN values returned by the GetSystemMetrics function.

One implication of this, is that the larger your console screen font is, the smaller you can make the buffersize.

As an example: here's how to get the minimum width of a console screen. I'm P/Invoking the GetSystemMetrics function from User32.dll using this advanced function (New-PInvoke by Joel Bennett).

$SM_CXMIN =28 # "The minimum width of a window, in pixels." enum value
New-PInvoke -Library User32.dll -Signature "int GetSystemMetrics(uint Metric)"
GetSystemMetrics $SM_CXMIN # returns 132 on my system

To retrieve the size of the font used by the console screen buffer, try GetConsoleFontSize from kernel32.dll.

Note:

The value returned by GetSystemMetrics $SM_CXMIN is the total width (including the borders) of the console screen.

OTHER TIPS

Maybe I'm in wrong but with

[system.console]::BufferWidth 

you get the actual buffer width size.

This value can't be less than the current [System.Console]::WindowWidth size (will throw an exception).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top