Resizing the PowerShell Console
Windows 10's support for high DPI displays is much better than previous iterations of Windows, but there are still some times it gets a bit confused. One such problem occurs when you have multiple high DPI displays or two displays of different sizes. If you move PowerShell console windows between displays or log back in after being logged out for a while, you can end up with a scrunched up PowerShell window. Nothing I had to deal with when all I had was a pair of standard FullHD monitors, but ever since I got my Surface Book, and connected it to a 28 inch 4k monitor, I've had periodic problems. Very annoying when your PowerShell window changes to 37 characters wide and 7 lines long!
The fix is to reset the window size. Now I can do this graphically (right click on the title bar, select Properties, and then the Layout tab), but that's a nuisance at best, and besides, the whole idea of using the GUI to fix a console just isn't right. The answer is to leverage the built-in $host variable:
$host | Get-Member TypeName: System.Management.Automation.Internal.Host.InternalHost Name MemberType Definition ---- ---------- ---------- EnterNestedPrompt Method void EnterNestedPrompt() Equals Method bool Equals(System.Object obj) ExitNestedPrompt Method void ExitNestedPrompt() GetHashCode Method int GetHashCode() GetType Method type GetType() NotifyBeginApplication Method void NotifyBeginApplication() NotifyEndApplication Method void NotifyEndApplication() PopRunspace Method void PopRunspace(), void IHostSupportsInteractiveSession.PopRunspace() PushRunspace Method void PushRunspace(runspace runspace), void IHostSupportsInteractiveSession.PushRunspace(runspace runspace) SetShouldExit Method void SetShouldExit(int exitCode) ToString Method string ToString() CurrentCulture Property cultureinfo CurrentCulture {get;} CurrentUICulture Property cultureinfo CurrentUICulture {get;} DebuggerEnabled Property bool DebuggerEnabled {get;set;} InstanceId Property guid InstanceId {get;} IsRunspacePushed Property bool IsRunspacePushed {get;} Name Property string Name {get;} PrivateData Property psobject PrivateData {get;} Runspace Property runspace Runspace {get;} UI Property System.Management.Automation.Host.PSHostUserInterface UI {get;} Version Property version Version {get;}
OK, there's some interesting bits there, but the one that looks most promising is UI. So:
$host.UI | Get-Member TypeName: System.Management.Automation.Internal.Host.InternalHostUserInterface Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() Prompt Method System.Collections.Generic.Dictionary[string,psobject] Prompt(string caption, string message, System.Collection... PromptForChoice Method int PromptForChoice(string caption, string message, System.Collections.ObjectModel.Collection[System.Management... PromptForCredential Method pscredential PromptForCredential(string caption, string message, string userName, string targetName), pscredent... ReadLine Method string ReadLine() ReadLineAsSecureString Method securestring ReadLineAsSecureString() ToString Method string ToString() Write Method void Write(string value), void Write(System.ConsoleColor foregroundColor, System.ConsoleColor backgroundColor, ... WriteDebugLine Method void WriteDebugLine(string message) WriteErrorLine Method void WriteErrorLine(string value) WriteInformation Method void WriteInformation(System.Management.Automation.InformationRecord record) WriteLine Method void WriteLine(), void WriteLine(string value), void WriteLine(System.ConsoleColor foregroundColor, System.Cons... WriteProgress Method void WriteProgress(long sourceId, System.Management.Automation.ProgressRecord record) WriteVerboseLine Method void WriteVerboseLine(string message) WriteWarningLine Method void WriteWarningLine(string message) RawUI Property System.Management.Automation.Host.PSHostRawUserInterface RawUI {get;} SupportsVirtualTerminal Property bool SupportsVirtualTerminal {get;}
Hmmm. Even more interesting stuff. I can tell I'm going to be doing some poking around in here! But, for our purposes, let's take a look at RawUI.
That looks the most promising:
$host.UI.RawUI | Get-Member TypeName: System.Management.Automation.Internal.Host.InternalHostRawUserInterface Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) FlushInputBuffer Method void FlushInputBuffer() GetBufferContents Method System.Management.Automation.Host.BufferCell[,] GetBufferContents(System.Management.Automation.Host.Rectangle r) GetHashCode Method int GetHashCode() GetType Method type GetType() LengthInBufferCells Method int LengthInBufferCells(string str), int LengthInBufferCells(string str, int offset), int LengthInBufferCells(cha... NewBufferCellArray Method System.Management.Automation.Host.BufferCell[,] NewBufferCellArray(string[] contents, System.ConsoleColor foregro... ReadKey Method System.Management.Automation.Host.KeyInfo ReadKey(System.Management.Automation.Host.ReadKeyOptions options), Syst... ScrollBufferContents Method void ScrollBufferContents(System.Management.Automation.Host.Rectangle source, System.Management.Automation.Host.C... SetBufferContents Method void SetBufferContents(System.Management.Automation.Host.Coordinates origin, System.Management.Automation.Host.Bu... ToString Method string ToString() BackgroundColor Property System.ConsoleColor BackgroundColor {get;set;} BufferSize Property System.Management.Automation.Host.Size BufferSize {get;set;} CursorPosition Property System.Management.Automation.Host.Coordinates CursorPosition {get;set;} CursorSize Property int CursorSize {get;set;} ForegroundColor Property System.ConsoleColor ForegroundColor {get;set;} KeyAvailable Property bool KeyAvailable {get;} MaxPhysicalWindowSize Property System.Management.Automation.Host.Size MaxPhysicalWindowSize {get;} MaxWindowSize Property System.Management.Automation.Host.Size MaxWindowSize {get;} WindowPosition Property System.Management.Automation.Host.Coordinates WindowPosition {get;set;} WindowSize Property System.Management.Automation.Host.Size WindowSize {get;set;} WindowTitle Property string WindowTitle {get;set;}
BINGO! I see BufferSize and WindowSize, and I know from the GUI Properties page that those are the relevant settings, but just to verify:
$host.UI.RawUI.BufferSize | Get-Member TypeName: System.Management.Automation.Host.Size Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() Height Property int Height {get;set;} Width Property int Width {get;set;} $host.UI.RawUI.WindowSize | Get-Member TypeName: System.Management.Automation.Host.Size Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() Height Property int Height {get;set;} Width Property int Width {get;set;}
And there we have it. Both of them can be retrieved and set. So, I came up with a little script, Set-myConSize, that lets me restore the window to its default size, or set it to a new size if I'm doing something that needs a bit of window size tweaking.
<# .Synopsis Resets the size of the current console window .Description Set-myConSize resets the size of the current console window. By default, it sets the windows to a height of 40 lines, with a 3000 line buffer, and sets the the width and width buffer to 120 characters. .Example Set-myConSize Restores the console window to 120x40 .Example Set-myConSize -Height 30 -Width 180 Changes the current console to a height of 30 lines and a width of 180 characters. .Parameter Height The number of lines to which to set the current console. The default is 40 lines. .Parameter Width The number of characters to which to set the current console. Default is 120. Also sets the buffer to the same value .Inputs [int] [int] .Notes Author: Charlie Russel Copyright: 2017 by Charlie Russel : Permission to use is granted but attribution is appreciated Initial: 28 April, 2017 (cpr) ModHist: : #> [CmdletBinding()] Param( [Parameter(Mandatory=$False,Position=0)] [int] $Height = 40, [Parameter(Mandatory=$False,Position=1)] [int] $Width = 120 ) $Console = $host.ui.rawui $Buffer = $Console.BufferSize $ConSize = $Console.WindowSize # If the Buffer is wider than the new console setting, first reduce the buffer, then do the resize If ($Buffer.Width -gt $Width ) { $ConSize.Width = $Width $Console.WindowSize = $ConSize } $Buffer.Width = $Width $ConSize.Width = $Width $Buffer.Height = 3000 $Console.BufferSize = $Buffer $ConSize = $Console.WindowSize $ConSize.Width = $Width $ConSize.Height = $Height $Console.WindowSize = $ConSize
One quick comment on this script -- you can't set the BufferSize to smaller than the current WindowSize. With a Height buffer set to 3,000, that's not likely to be a problem, but if you don't want scroll bars on the bottom of your console windows (and you do NOT, trust me!), then you need the console WindowSize.Width to be the same as the BufferSize.Width. So if your reducing, you need to change the WindowSize first, then you can reduce the BufferSize. If you're increasing width, you need to do the buffer first.
Finally, I set an alias in my $Profile:
Set-Alias -Name Resize -Value Set-myConSize
Configuring Windows Server 2016 Core with and for PowerShell
I know I owe you more on creating a lab with PowerShell, and I'll get to that in a few days. But having just set up a new domain controller running Server 2016 Core, I thought I'd include a couple of tricks I worked through to make your life a little easier if you choose to go with core.
First: Display Resolution -- the default window for a virtual machine connected with a Basic Session in VMConnect is 1024x768. Which is just too small. So, we need to change that. Now in the full Desktop Experience, you'd right click and select Display Resolution, but that won't work in Server Core, obviously. Instead we have PowerShell. Of course. The command to set the display resolution to 1600x900 is:
Set-DisplayResolution -Width 1600 -Height 900
This will accept a -Force parameter if you don't like being prompted. A key point, however, is that it ONLY accepts supported resolutions. For a Hyper-V VM, that means one of the following resolutions:
1920x1080 1600x1050 1600x1200 1600x900 1440x900 1366x768 1280x1024 1280x800 1280x720 1152x864 1024x768 800x600
Now that we have a large enough window to get something done, start PowerShell with the Start PowerShell (that space is on purpose, remember we're still in a cmd window.) But don't worry, we'll get rid of that cmd window shortly.
Now that we have a PowerShell window, you can set various properties of that window by using any of the tricks I've shown before, such as Starting PowerShell Automatically which sets the Run key to start PowerShell for the current user on Login with:
New-ItemProperty HKCU:\Software\Microsoft\Windows\CurrentVersion\Run ` -Name "Windows PowerShell" ` -Value "C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell.exe"
I also showed you how to set the PowerShell window size, font, etc in Starting PowerShell Automatically Revisited. And, of course, you can set the PowerShell window colour and syntax highlighting colours as described in Setting Console Colours. Of course, all my $Profile tricks work as well, so check those out.
So, now that we've configured the basics of our PowerShell windows, let's set PowerShell to replace cmd as the default console window. To do that, use the Set-ItemProperty cmdlet to change the WinLogon registry key:
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' ` -Name Shell ` -Value 'PowerShell.exe -NoExit'
Viola! Now, when we log on to our Server Core machine, it will automatically open a pair of PowerShell windows, one from WinLogon registry key and one from the Run registry key.
Setting Console Colours
As I described in my previous post, I always open both an admin and non-admin PowerShell window when I log on to a computer. To tell the two apart, I set the background colour of the Admin window to dark red, with white text, and the non-admin window to a white background with dark blue text. The result is clearly and immediately different, warning me when I'm running as an administrator. To do that, I use the following:
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent() $p = New-Object system.security.principal.windowsprincipal($id) # Find out if we're running as admin (IsInRole). # If we are, set $Admin = $True. if ($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)){ $Admin = $True } else { $Admin = $False } if ($Admin) { $effectivename = "Administrator" $host.UI.RawUI.Backgroundcolor="DarkRed" $host.UI.RawUI.Foregroundcolor="White" clear-host } else { $effectivename = $id.name $host.UI.RawUI.Backgroundcolor="White" $host.UI.RawUI.Foregroundcolor="DarkBlue" clear-host }
That works great for older versions of the Windows PowerShell console, but starting with PowerShell v5, that can have an unintended side effect. In PowerShell v5, we now have PSReadLine that does context sensitive colouration of the command line. But the default PowerShell console has a dark blue background, with white text. And when I changed the background colour of my non-admin PowerShell window to white, it gets a little hard to read!! So, to fix that, I use Set-PSReadLineOption to change the various kinds of context sensitive colour changes to something that works with a light background. We don't want to do that for the dark red background of the Admin window, so we'll need to check which colour we are and adjust accordingly.
First, get the current colour:
$pData = (Get-Host).PrivateData $curForeground = [console]::ForegroundColor $curBackground = [console]::BackgroundColor
You'll only want to configure the context sensitive colouring options if you're running on Windows 10 or Server 2016. Prior versions of Windows didn't have the new system console that comes with Windows 10. So you'll want to check that the build number is > 10240
$Build = (Get-WmiObject Win32_OperatingSystem).BuildNumber
If $Build -ge 10240, then set the various context sensitive tokens to work with the colour we have.
# PowerShell v5 uses PSReadLineOptions to do syntax highlighting. # Base the color scheme on the background color If ( $curBackground -eq "White" ) { Set-PSReadLineOption -TokenKind None -ForegroundColor DarkBlue -BackgroundColor White Set-PSReadLineOption -TokenKind Comment -ForegroundColor DarkGray -BackgroundColor White Set-PSReadLineOption -TokenKind Keyword -ForegroundColor DarkGreen -BackgroundColor White Set-PSReadLineOption -TokenKind String -ForegroundColor Blue -BackgroundColor White Set-PSReadLineOption -TokenKind Operator -ForegroundColor Black -BackgroundColor White Set-PSReadLineOption -TokenKind Variable -ForegroundColor DarkCyan -BackgroundColor White Set-PSReadLineOption -TokenKind Command -ForegroundColor DarkRed -BackgroundColor White Set-PSReadLineOption -TokenKind Parameter -ForegroundColor DarkGray -BackgroundColor White Set-PSReadLineOption -TokenKind Type -ForegroundColor DarkGray -BackgroundColor White Set-PSReadLineOption -TokenKind Number -ForegroundColor Red -BackgroundColor White Set-PSReadLineOption -TokenKind Member -ForegroundColor DarkBlue -BackgroundColor White $pData.ErrorForegroundColor = "Red" $pData.ErrorBackgroundColor = "Gray" $pData.WarningForegroundColor = "DarkMagenta" $pData.WarningBackgroundColor = "White" $pData.VerboseForegroundColor = "DarkYellow" $pData.VerboseBackgroundColor = "DarkCyan" } elseif ($curBackground -eq "DarkRed") { Set-PSReadLineOption -TokenKind None -ForegroundColor White -BackgroundColor DarkRed Set-PSReadLineOption -TokenKind Comment -ForegroundColor Gray -BackgroundColor DarkRed Set-PSReadLineOption -TokenKind Keyword -ForegroundColor Yellow -BackgroundColor DarkRed Set-PSReadLineOption -TokenKind String -ForegroundColor Cyan -BackgroundColor DarkRed Set-PSReadLineOption -TokenKind Operator -ForegroundColor White -BackgroundColor DarkRed Set-PSReadLineOption -TokenKind Variable -ForegroundColor Green -BackgroundColor DarkRed Set-PSReadLineOption -TokenKind Command -ForegroundColor White -BackgroundColor DarkRed Set-PSReadLineOption -TokenKind Parameter -ForegroundColor Gray -BackgroundColor DarkRed Set-PSReadLineOption -TokenKind Type -ForegroundColor Magenta -BackgroundColor DarkRed Set-PSReadLineOption -TokenKind Number -ForegroundColor Yellow -BackgroundColor DarkRed Set-PSReadLineOption -TokenKind Member -ForegroundColor White -BackgroundColor DarkRed $pData.ErrorForegroundColor = "Yellow" $pData.ErrorBackgroundColor = "DarkRed" $pData.WarningForegroundColor = "Magenta" $pData.WarningBackgroundColor = "DarkRed" $pData.VerboseForegroundColor = "Cyan" $pData.VerboseBackgroundColor = "DarkRed" } }
Finally, let's make sure that console window is the right size, and while we're at it, set the window title. (This is a workaround for a PITA bug in recent builds of Windows 10/Server 2016 that seems to have problems setting the console window size and keeping it!)
$host.ui.rawui.WindowTitle = $effectivename + "@" + $HostName +" >" $Host.UI.RawUI.WindowSize = New-Object System.Management.Automation.Host.Size(120,40)
Now, with all of this, you have effective, context-sensitive, command-line colouring of your PowerShell windows.
ETA: The Console as we know it is completely broken in recent builds of Windows 10. As of August, 2018, you can't effectively set the background color of the console and have it stick, plus they completely changed the behaviour of Set-PSReadlineOption by changing the parameters, and removing support for -TokenKind with FG/BG colors to totally break this script. Sigh.