Playing with Window Size

$host isn't just for EnterNestedPrompt().  $host.UI.RawUI has some intresting data.  For example, Nicely Wrapping a Line of Text to STDOUT uses $host.UI.RawUI.WindowSize.Width to determine where to break the line based on window width.  However,

$host.UI.RauUI.WindowSize.Width = 80;

doesn't seem to work.  The reason is because 'WindowSize' is a System.Management.Automation.Host.Size object, and Width is that object's Width property.  Therefore, to change the window's size, a new System.Management.Automation.Host.Size object must be created.  New ... object.  Hm.

$host.UI.RawUI.WindowSize = New-Object System.Management.Automation.Host.Size(80, 25);

As for the scrollbar, that's the $host.UI.RawUI.BufferSize property, which is another Host.Size object.

$host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size(80, 9999);

The one gotcha is, when growing the window's width, set the BufferSize first, then the WindowSize.  When shrinking the width, set the WindowSize first, then the BufferSize.  As a mnemoic, BufferSize -ge WindowSize must be true.

Comments (0)

Skip to main content