Part of day job involves creating training material, often in the form of video training for Pluralsight or articles for Petri.com. Since I usually am covering PowerShell I often need to capture a PowerShell session. And sometimes I want the screen to be a particular size. So over time I've created a few PowerShell tools to resize console and application windows. The PowerShell console window stores its dimension under $host.ui.rawui.windowsize.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
These are the same settings you would see here:
As long as you use a value less than the buffer dimensions, you can modify the console window from a prompt. But it takes a step you might not realize. You can't do this:
$host.ui.rawui.WindowSize.Height = 40
Instead, you can create a new type of object with your intended dimensions.
$size = New-Object System.Management.Automation.Host.Size(100,25)
Then you can use this object as a value for the WindowSize property.
$host.ui.rawui.WindowSize = $size
Naturally, I created a function to do this for me.
#requires -version 3.0 #not for the ISE Function Set-ConsoleWindowSize { [cmdletbinding(SupportsShouldProcess)] Param( [Alias("x")] [ValidateScript({$_ -ge 10 -AND $_ -le $host.ui.rawui.buffersize.width})] [int]$Width = $host.ui.rawui.windowsize.width, [Alias("y")] [ValidateScript({$_ -ge 10 -AND $_ -le $host.ui.rawui.buffersize.height})] [int]$Height = $host.ui.rawui.windowsize.height ) write-verbose "Setting dimensions to $width x $height" $size = New-Object System.Management.Automation.Host.Size($Width,$Height) #WhatIf code if ($PSCmdlet.ShouldProcess("$width by $height")) { $host.ui.rawui.WindowSize = $size } }
My function also includes code to support –WhatIf.
Of course now that I've shown you that I have an alternative. You can use the .NET class [System.Console] which has properties for width and height. And you can set these values independently.
[system.console]::WindowHeight = 20
You can't discover this unless you know something of the .NET Framework, but you could have discovered $host which is why I showed you that first. Since I often need to record video at 1280x720 dimensions, I wrote a quick and dirty script to set my PowerShell console window to those dimensions.
#requires -version 3.0 #set PowerShell console to 1280x720 #This is NOT for the ISE [cmdletbinding(SupportsShouldProcess)] Param() $h = 33 $w = 103 Write-Verbose "Setting Height to $h" Write-Verbose "Setting Width to $w" if ($PSCmdlet.shouldProcess("current session")) { [system.console]::WindowHeight = $h [system.console]::WindowWidth = $w }
Everything I've shown you so far is for the PowerShell console. But what about the ISE? You can't use the techniques I've covered. Application windows are bit more complicated and I'm not going to go into the details. But I came across some code on GitHub (https://gist.github.com/coldnebo/1148334). I don't do Minecraft but it didn't take much to turn it into a re-usable function.
#requires -version 3.0 #from https://gist.github.com/coldnebo/1148334 Function Set-WindowSize { [cmdletbinding(SupportsShouldProcess)] Param( [Parameter(Position=0,HelpMessage="What is the MainWindowHandle?", ValueFromPipeline,ValueFromPipelineByPropertyName)] [ValidateNotNullorEmpty()] [Alias("handle")] [System.IntPtr]$MainWindowHandle = (Get-Process -id $pid).MainWindowHandle, [Alias("x")] [int]$Width = 1280, [Alias("y")] [int]$Height = 720 ) Begin { Write-Verbose "Starting $($MyInvocation.Mycommand)" Write-Verbose "Adding type" Add-Type @" using System; using System.Runtime.InteropServices; public class Win32 { [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); } public struct RECT { public int Left; // x position of upper-left corner public int Top; // y position of upper-left corner public int Right; // x position of lower-right corner public int Bottom; // y position of lower-right corner } "@ } #begin Process { #verify handle and get process Write-Verbose "Verifying process with MainWindowHandle of $MainWindowhandle" $proc = (Get-Process).where({$_.mainwindowhandle -eq $MainWindowhandle}) if (-NOT $proc.mainwindowhandle) { Write-Warning "Failed to find a process with a MainWindowHandle of $MainWndowhandle" #bail out Return } Write-Verbose "Creating rectangle objects" $rcWindow = New-Object RECT $rcClient = New-Object RECT Write-Verbose "Getting current settings" [Win32]::GetWindowRect($MainWindowHandle,[ref]$rcWindow) | Out-Null [Win32]::GetClientRect($MainWindowHandle,[ref]$rcClient) | Out-Null Write-Verbose "Setting new coordinates" #WhatIf if ($PSCmdlet.ShouldProcess("$($proc.MainWindowTitle) to $width by $height")) { $dx = ($rcWindow.Right - $rcWindow.Left) - $rcClient.Right $dy = ($rcWindow.Bottom - $rcWindow.Top) - $rcClient.Bottom Write-Verbose "Moving window" [Win32]::MoveWindow($MainWindowHandle, $rct.Left, $rct.Top, $width + $dx, $height + $dy, $true ) | out-null } #close Whatif } #process End { Write-Verbose "Ending $($MyInvocation.Mycommand)" } #end } #end Set-WindowSize function
The code supports –WhatIf and defaults to the current application, which is presumably the PowerShell ISE.
But this is what actually gets set.
So if you wanted to include the title bar you would need to adjust accordingly.
All of this may not really be applicable to your work, but if you find a good use I hope you'll let me know. Have a great weekend.
# Add this to your $PROFILE
$WindowSize = $Host.UI.RawUI.MaxPhysicalWindowSize
[system.console]::SetWindowPosition(0,0)
[system.console]::SetWindowSize(($WindowSize.Width -3),$WindowSize.Height)
[system.console]::SetBufferSize(($WindowSize.Width -3),3000)