Being able to change the size of the console window (or any window for that matter) are quick and easy to do using the mouse. The same for changing the window state (for example maximizing a window). But if you want to do this programmatically (for whatever reason) it’s not so simple.
I have created a small function that makes this much easier to accomplish.
The main function (Resize-Console) lets you set a custom Width and Height for the console window, as well as maximize the console, or restore it if it’s already maximized. Note that the custom size only works in a console window, so if you try this in PowerShell ISE it will give you a warning and abort.
The maximize and restore functionality on the other hand, works in both the console and in ISE.
I have also included a couple of small helper functions. The first, Get-ConsoleSize, will just output the current size of the console window, while Get-WindowState will return either ‘Normal‘ or ‘Maximized‘ based on the current state of the window.
As always, holler if you spot any bugs, or have suggestions for future improvements.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Resize-Console { | |
<# | |
.SYNOPSIS | |
Resize the PowerShell console programmatically. | |
.DESCRIPTION | |
This function lets you resize the PowerShell console window by setting | |
custom width and height properties, as well as maximize and restore the | |
console window. | |
.EXAMPLE | |
Resize-Console 200 50 | |
.EXAMPLE | |
Resize-Console -Maximize | |
.EXAMPLE | |
Resize-Console -Restore | |
.EXAMPLE | |
if (Get-WindowState -eq 'Normal') {Resize-Console -Maximize} | |
.NOTES | |
Author: Øyvind Kallstad | |
Date: 11.10.2015 | |
Version: 1.0 | |
.LINK | |
https://communary.wordpress.com/ | |
#> | |
[CmdletBinding(DefaultParameterSetName = 'CustomSize')] | |
param ( | |
# Set the window width. | |
[Parameter(ParameterSetName = 'CustomSize', Position = 0)] | |
[ValidateRange(1,[int]::MaxValue)] | |
[int] $Width = $host.UI.RawUI.WindowSize.Width, | |
# Set the window height. | |
[Parameter(ParameterSetName = 'CustomSize', Position = 1)] | |
[ValidateRange(1,[int]::MaxValue)] | |
[int] $Height = $host.UI.RawUI.WindowSize.Height, | |
# Maximize the window. | |
[Parameter(ParameterSetName = 'Maximize')] | |
[switch] $Maximize, | |
# Restore the window (if maximized). | |
[Parameter(ParameterSetName = 'Restore')] | |
[switch] $Restore | |
) | |
if(($PSCmdlet.ParameterSetName -eq 'Maximize') -or ($PSCmdlet.ParameterSetName -eq 'Restore')) { | |
$Win32ShowWindowAsync = Add-Type –memberDefinition @” | |
[DllImport("user32.dll")] | |
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); | |
“@ –name 'Win32ShowWindowAsync' –namespace Win32Functions –passThru | |
# get main window handle of the current process | |
$MainWindowHandle = (Get-Process –id $pid).MainWindowHandle | |
if ($Maximize) { | |
$Win32ShowWindowAsync::ShowWindowAsync($MainWindowHandle, 3) | Out-Null | |
} | |
if ($Restore) { | |
$Win32ShowWindowAsync::ShowWindowAsync($MainWindowHandle, 9) | Out-Null | |
} | |
} | |
else { | |
if ($host.Name -eq 'ConsoleHost') { | |
# buffer size can't be smaller than window size | |
if ($Width -gt $host.UI.RawUI.BufferSize.Width) { | |
$host.UI.RawUI.BufferSize = New-Object –TypeName System.Management.Automation.Host.Size –ArgumentList ($Width, $host.UI.RawUI.BufferSize.Height) | |
} | |
# if width is too large, set to max allowed size | |
if ($Width -gt $host.UI.RawUI.MaxPhysicalWindowSize.Width) { | |
$Width = $host.UI.RawUI.MaxPhysicalWindowSize.Width | |
} | |
# if height is too large, set to max allowed size | |
if ($Height -gt $host.UI.RawUI.MaxPhysicalWindowSize.Height) { | |
$Height = $host.UI.RawUI.MaxPhysicalWindowSize.Height | |
} | |
# set window size | |
$host.UI.RawUI.WindowSize = New-Object –TypeName System.Management.Automation.Host.Size –ArgumentList ($Width, $Height) | |
} | |
else { | |
Write-Warning 'Setting the console window size is only supported when running in the console host.' | |
} | |
} | |
} | |
function Get-ConsoleSize { | |
Write-Output ($host.UI.RawUI.WindowSize) | |
} | |
function Get-WindowState { | |
$Win32IsZoomed = Add-Type –memberDefinition @” | |
[DllImport("user32.dll")] | |
public static extern bool IsZoomed(IntPtr hWnd); | |
“@ –name 'Win32IsZoomed' –namespace Win32Functions –passThru | |
if ($Win32IsZoomed::IsZoomed(((Get-Process –id $pid).MainWindowHandle))) { | |
Write-Output 'Maximized' | |
} | |
else { | |
Write-Output 'Normal' | |
} | |
} |