How-to: Set the size of the console window [Set-ConsoleSize.ps1]

The size of the PowerShell console window can be manipulated using the $host variable.

The height, width and buffer size can all be set independently, however the buffer size must at all times be at least as large as the console window. When making the window larger, modify the buffer first, when making the window smaller, modify the buffer last.
The script below does this for you.

This only affects the current console, i.e. it will not affect the default size/layout for subsequent new PowerShell sessions.

There is a minor bug in Windows 10 post-1809 update, that causes the window to scroll down after any console resizing, you may want to hold off on using this script until that issue is fixed.

Set the size of the current console window

Set-ConsoleSize sets or resets the size of the current console window. By default, it
sets the window to a height of 40 lines, with a 2000 line buffer, and sets the 
the width and width buffer to 120 characters. 

Restore the console window to 40h x 120w:

Change the current console to a height = 30 lines and a width = 180 chars:
Set-ConsoleSize -Height 30 -Width 180

.Parameter Height
The number of lines to which to set the current console. Default = 40 lines. 

.Parameter Width
The number of characters to which to set the current console. Default = 120 chars.


 Last edit: 2019-08-29
     [int]$Height = 40,
     [int]$Width = 120
$console = $host.ui.rawui
$ConBuffer  = $console.BufferSize
$ConSize = $console.WindowSize

$currWidth = $ConSize.Width
$currHeight = $ConSize.Height

# if height is too large, set to max allowed size
if ($Height -gt $host.UI.RawUI.MaxPhysicalWindowSize.Height) {
    $Height = $host.UI.RawUI.MaxPhysicalWindowSize.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 the Buffer is wider than the new console setting, first reduce the width
If ($ConBuffer.Width -gt $Width ) {
   $currWidth = $Width
# If the Buffer is higher than the new console setting, first reduce the height
If ($ConBuffer.Height -gt $Height ) {
    $currHeight = $Height
# initial resizing if needed
$host.UI.RawUI.WindowSize = New-Object System.Management.Automation.Host.size($currWidth,$currHeight)

# Set the Buffer
$host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.size($Width,2000)

# Now set the WindowSize
$host.UI.RawUI.WindowSize = New-Object System.Management.Automation.Host.size($Width,$Height)

# Display the new sizes (Optional/for debugging)
# "Height: " + $host.ui.rawui.WindowSize.Height
# "Width:  " + $host.ui.rawui.WindowSize.width


PS C:\> Set-ConsoleSize -Height 30 -Width 180

Calling from another script saved in the same directory:

#Requires -Version 3.0
& "$PSScriptRoot\set-consolesize.ps1" -height 25 -width 90

“To take the measure of oneself by reference to one’s colleagues leads to envy or complacency rather than constructive self-examination” ~ Benno C. Schmidt, Jr.

Related PowerShell Cmdlets:

Clear-Host - Clear the screen.
Equivalent CMD command: MODE - Configure a system device.

Copyright © 1999-2021
Some rights reserved