Recently, I wanted a PowerShell script to get the Windows size to display output correctly. In this tutorial, I will explain how to get and set the window size in PowerShell using various methods.
By default, the PowerShell console window has a predefined size, but you might need to adjust it for better readability or to fit specific output requirements.
Get the Current Window Size in PowerShell
To get the current size of the PowerShell window, you can use the following script. This script retrieves the width and height of the console window.
$host.UI.RawUI.WindowSizeThis command returns an object with the properties Width and Height, which represent the current dimensions of the PowerShell window.
Example: Displaying Current Window Size
Let’s say you’re working on a server and you want to check the current PowerShell window size:
$windowSize = $host.UI.RawUI.WindowSize
Write-Output "Current Window Size: Width = $($windowSize.Width), Height = $($windowSize.Height)"I executed the above PowerShell script in the local system, and you can see the exact output in the screenshot below:

Check out Get Window Titles Using PowerShell
Set the Window Size in PowerShell
To set the PowerShell window size, you can modify the WindowSize property of the RawUI object. Here’s a script to set the window size to a specific width and height.
Example: Setting Window Size to 120×40
Suppose you want to set the PowerShell window size to a width of 120 and a height of 40. You can use the following script:
$size = $host.UI.RawUI.WindowSize
$size.Width = 120
$size.Height = 40
$host.UI.RawUI.WindowSize = $sizeThis script first retrieves the current window size, modifies the width and height, and then applies the new size to the PowerShell window.
Handle Errors and Exceptions
When changing the window size, it’s important to handle potential errors. For instance, setting a window size larger than the screen resolution can cause issues. Here’s how you can handle such exceptions:
try {
$size = $host.UI.RawUI.WindowSize
$size.Width = 120
$size.Height = 40
$host.UI.RawUI.WindowSize = $size
Write-Output "Window size set to Width = 120, Height = 40"
} catch {
Write-Error "Failed to set window size: $_"
}Check out Get Windows Services Using PowerShell
Maximize the PowerShell Window
If you want to maximize the PowerShell window, you can use the following script. This script calculates the maximum possible window size based on the screen resolution.
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Screen {
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
public struct RECT {
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public static RECT GetWindowRect() {
IntPtr hWnd = GetForegroundWindow();
RECT rect = new RECT();
GetWindowRect(hWnd, ref rect);
return rect;
}
}
"@
$rect = [Screen]::GetWindowRect()
$width = $rect.Right - $rect.Left
$height = $rect.Bottom - $rect.Top
$size = $host.UI.RawUI.WindowSize
$size.Width = [Math]::Min($width, $host.UI.RawUI.MaxPhysicalWindowSize.Width)
$size.Height = [Math]::Min($height, $host.UI.RawUI.MaxPhysicalWindowSize.Height)
$host.UI.RawUI.WindowSize = $sizeThis script uses Windows API calls to get the screen resolution and then sets the PowerShell window size to the maximum possible dimensions.
Check out Get Windows Activation Status Using PowerShell
Set Window Size and Position using PowerShell
In addition to setting the window size, you might also want to set the position of the PowerShell window on the screen. Here’s a script to set both the size and position:
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class User32 {
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
}
"@
$hWnd = [User32]::GetForegroundWindow()
$null = [User32]::SetWindowPos($hWnd, [IntPtr]::Zero, 100, 100, 800, 600, 0)
$size = $host.UI.RawUI.WindowSize
$size.Width = 100
$size.Height = 40
$host.UI.RawUI.WindowSize = $sizeThis script sets the PowerShell window to a size of 800×600 pixels and positions it at coordinates (100, 100) on the screen.
Automate Window Size for Scripts
Automating the window size can save time if you frequently run scripts that produce large amounts of output. For instance, if you manage multiple servers and need to monitor log files, you can set the window size to display more lines of text:
$size = $host.UI.RawUI.WindowSize
$size.Width = 150
$size.Height = 50
$host.UI.RawUI.WindowSize = $size
# Run your log monitoring script
Get-Content -Path "C:\Logs\ServerLog.txt" -WaitCheck out List Printers Using PowerShell
Enhance Windows Size For Presentations and Demos
When presenting PowerShell scripts to an audience, a larger window size can make the text more readable. Here’s a script to maximize the window size for presentations:
$size = $host.UI.RawUI.WindowSize
$size.Width = $host.UI.RawUI.MaxPhysicalWindowSize.Width
$size.Height = $host.UI.RawUI.MaxPhysicalWindowSize.Height
$host.UI.RawUI.WindowSize = $sizeConclusion
In this tutorial, I explained how to get and set Windows size using PowerShell. I hope these scripts will be helpful.
You may also like the following tutorials:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.