Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 sometimes I get the error on certain desktop sizes. For example, I tried with 95 and failed with the 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 maximum window size setting on the machine that it is running the script on and set the size of PowerShell window?

like image 940
Krishna Avatar asked Aug 17 '16 14:08

Krishna


People also ask

How do I change the buffer size in PowerShell?

On an open PowerShell session, right-click the upper left corner and select Properties. Select the Layout tab. Change the height of the screen buffer size as I've done in Figure 1 and click OK.

What is @() in PowerShell script?

What is @() in PowerShell Script? In PowerShell, the array subexpression operator “@()” is used to create an array. To do that, the array sub-expression operator takes the statements within the parentheses and produces the array of objects depending upon the statements specified in it.

How do I fullscreen PowerShell?

Display Options -> Fullscreen, or ALT+ENTER should enter fullscreen mode.

Is PowerShell 64 or 32 bit?

When you run Windows PowerShell, the 64-bit version runs by default. However, you might occasionally need to run Windows PowerShell (x86), such as when you're using a module that requires the 32-bit version or when you're connecting remotely to a 32-bit computer.


1 Answers

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
like image 75
Austin T French Avatar answered Oct 26 '22 11:10

Austin T French