user2344145 user2344145 - 3 months ago 43
PowerShell Question

Window Size in Powershell

I'm trying to set size of powershell window using a powershell script. The code I'm using is

$pshost = Get-Host

$psWindow = $pshost.UI.RawUI

$newSize =$psWindow.BufferSize

$newSize.Height = 4000
$newSize.Width = 200

$psWindow.BufferSize = $newSize

$newSize = $psWindow.WindowSize
$newSize.Height = 95
$newSize.Width = 150

$psWindow.WindowSize= $newSize


It works fine in most cases but some times I get the error on certain desktop sizes. For Example, I tried with 95 and failed with error below for my desktop screen size 1440x960.

Exception setting "WindowSize": "Window cannot be taller than 82.
Parameter name: value.Height
Actual value was 95."
At line:1 char:5
+ $psWindow.WindowSize= $newSize
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting


Is there a way I can calculate the max window size setting on machine that is running the script and set the size of powershell window?

Answer

Your were already on the right course.

(get-host).UI.RawUI.MaxWindowSize

Or more specifically:

$height = (get-host).UI.RawUI.MaxWindowSize.Height
$width = (get-host).UI.RawUI.MaxWindowSize.Width