Getting normality back
The PowerShelling that I do mostly involves messing around the console window – dimensions, colours, title, etc.
I wanted an easy way to get normality back so I added this to my $PROFILE
. Nothing fancy or clever, but here it is:
#------------------------------------------------------------------------------ # Save default console state - do it in a script block so as not to # polute global space with the $ui variable &{ $ui=$Host.UI.RawUI if ($ui) { $global:__saved_state = @{ "wt" = $ui.WindowTitle "bw" = $ui.BufferSize.Width "bh" = $ui.BufferSize.Height "ww" = $ui.WindowSize.Width "wh" = $ui.WindowSize.Height "fg" = $ui.ForegroundColor "bg" = $ui.BackgroundColor "cs" = $ui.CursorSize } } } # now a function to reset it function Reset-Host { $ss = $global:__saved_state if ($ss) { $ui=$Host.UI.RawUI $bs=$ui.BufferSize $bw=[Math]::Max($ss.bw,$bs.Width) $bh=[Math]::Max($ss.bh,$bs.Height) $ui.BufferSize = New-Object Management.Automation.Host.Size $bw,$bh $ui.WindowSize = New-Object Management.Automation.Host.Size $ss.ww,$ss.wh $ui.BufferSize = New-Object Management.Automation.Host.Size $ss.bw,$ss.bh $ui.CursorSize=$ss.cs $ui.WindowTitle = $ss.wt $ui.ForegroundColor = $ss.fg $ui.BackgroundColor = $ss.bg Clear-Host } else { write-warning "No saved host state" } } # and a handy alias New-Alias reset Reset-Host
The weird bit with setting the buffer size twice is because you can’t set the buffer size smaller than the current window size.
So that’s it. Whenever the console is in a terrible state, I just type reset
(or Reset-Host
) and all is back to normal.
If I ever change my default console window settings through the shortcut that starts it, this will still work.
Comments are closed.