ps1.soapyfrog.com

doing inappropriate things with powershell

Pages

  • About
  • Downloads
  • Archives

Search

Popular Posts

  • Grrr 1.1 and Big Invaders
  • Space Invaders
  • Convert images to text (ASCII art)
  • Cmdlet clashes
  • Console screen grabs in html

Recent Posts

  • Grrr source code, including Invaders
  • Google going down the pan
  • Cmdlet clashes
  • Grrr 1.1 and Big Invaders
  • Grrr, Cmdlets and PSInvaders revival

Categories

  • Announce (7)
  • Cmdlets (2)
  • Cool (16)
  • Grrr (6)
  • Hint (2)
  • Invaders (5)
  • Odd (2)
  • PowerShell (27)
  • Quiz (3)
  • Rant (7)
  • Uncategorized (1)
  • Utility (5)

Months

  • August 2007 (1)
  • April 2007 (1)
  • March 2007 (1)
  • February 2007 (3)
  • January 2007 (25)
  • December 2006 (1)

Bookmarks

  • Blogroll
    • $script Fanatics
    • blog.soapyfrog.com
    • Brian Long
    • Lee Holmes
    • Nik Crabtree
    • PowerShell-Scripting (French)
    • Richy Rich
    • The PowerShell Guy
    • Windows PowerShell
  • Links
    • Carbon-neutral web hosting!

Meta

  • Log in
  • Posts RSS
  • Comments RSS
  • Valid XHTML
  • Valid CSS
« Space Invaders with sound!
Hashes and properties »
 

Getting normality back

14 Jan 2007 07:27 am// Cool, PowerShell    

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.

Copyright © 2006-2009 Adrian Milliner

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 License.