Archive for October 2014

A little of this, a little of that   Leave a comment

Last week I had a delightful time at the EU PowerShell summit in Amsterdam ( and the Dutch PowerShell User Group ( One of the talks I gave was a quick walk through of a dozen or so hidden PowerShell gems. I thought I would share some of those here. Here we go

Nearly every PowerShell user is aware of the $HOST automatic variable, but you might not be taking advantage of all of it’s coolness. Here’s a few things that you might not use, but might want to


I like a nice clean interface when I’m using the shell, with very little extraneous info, so I don’t have my path as part of the prompt (it’s just “PS>” if I’m a regular user and “PS#” in an elevated shell). I put my location in the window title by having my cd function include:

$HOST.UI.RawUI.WindowTitle = $PWD

However, $HOST can do a whole bunch more!


The screen is made up of buffercells which have the specific character, foreground and background colors which can be changed and replaced with other APIs on the host. The script below randomly changes the colors of characters on the screen, and then paints the entire window with an ‘X’ in every cell. The code is relatively simple, and while you should be able to just cut-and-paste running this as a script will be cleaner. Comments and code follow:

# A couple of limits
$xmax = 30
$ymax = 80
# clear up before the fun
# capture your current cursor position
$origin = $host.ui.rawui.CursorPosition
# put something on the screen
get-childitem $env:windir | select -first ($host.ui.rawui.windowsize.height-8)
# create a rectangle of part of the screen

$ul = new-object System.Management.Automation.Host.Coordinates 0,0
$lr = new-object System.Management.Automation.Host.Coordinates $ymax,$xmax
$r = new-object System.Management.Automation.Host.Rectangle $ul,$lr
# and capture the screen

$bc = $host.ui.rawui.GetBufferContents($r)
$cp = $host.ui.rawui.CursorPosition
$host.ui.rawui.CursorPosition  = $origin
# now for all of our buffer cells, paint the character with a new foreground color
# we’ll use write-host as it’s pretty because you can see the change as each character
# is painted

[consolecolor]$c = 1
foreach($x in 0..$xmax)
        foreach($y in 0..$ymax)
                $f = get-random 16
                write-host -for $f -no $bc[$x,$y].Character
        write-host ""
$host.ui.rawui.CursorPosition  = $cp
# before proceeding, get <ENTER>
read-host "Press Enter"

# Paint the screen with “X”
# determine the window size
$ws = $host.ui.rawui.windowsize
$ul = new-object System.Management.Automation.Host.Coordinates 0,0
$lr = new-object System.Management.Automation.Host.Coordinates $ws.width,$ws.height
$bc = $bc = new-object System.Management.Automation.Host.BufferCell
$bc.Character = "X"
$bc.ForegroundColor = "White"
$rect = new System.Management.Automation.Host.Rectangle
$rect.Bottom = $ws.height
$rect.Right = $ws.width
$coor = new-object System.Management.Automation.Host.Coordinates @(0,0)
$host.ui.rawui.CursorPosition = $coor
# set the entire buffer contents with our single buffer cell

This could be done differently, of course. The random colors could have been assigned in the buffercells and then written all at once with the SetBufferContents method.

I’ve used a similar technique to implement a curses library for PowerShell, I’ll show more of that in a later missive. Here’s a teaser:




Posted October 9, 2014 by jtruher3 in PowerShell