Modify the PowerShell Console Title and Display a Rabbit

ScriptingGuy1

Summary: Modify the Windows PowerShell console title and display a rabbit at random locations. The Microsoft Scripting Guys have fun.

Microsoft Scripting Guy Ed Wilson here. I love Microsoft Outlook 2010.

United States FTC disclaimer: The Microsoft Scripting Guys work for the Microsoft Corporation. I have been provided with a cooperate laptop that runs 64-bit Windows 7, and a 64-bit copy of Microsoft Office 2010 Professional Edition. In addition, the Microsoft Corporation provides me a salary, health insurance, bonus, U.S. national holidays off, and annual leave. The Microsoft Corporation, however, has not purchased for me a Bluetooth mouse or keyboard. Nor did the Microsoft Corporation purchase my Zune HD, which is currently blasting Buddy Guy. I also had to purchase the Buddy tracks.

One of the things I love so much about Microsoft Outlook 2010 is the ability to set reminders. I use the calendar feature extensively. For example, today is the birthday of the female scripter who hangs out with me at the Charlotte domicile. Last year, I took her to the Smokey Mountains for a woodworking class I was attending. She enjoys the mountains, but I think she probably would enjoy a different activity than my attending a woodworking class for her birthday. However, she seems to like the keepsake box I made for her. The trick is to do something nice for her without being caught in the prep work for the activity. To that end, I came up with the perfect present: a Windows PowerShell script! Everyone loves cool Windows PowerShell scripts, and this one is really cool. I hope she likes it. The complete Set-Rabbit.ps1 script is shown here.

Set-Rabbit.ps1

function global:set-ConsolePosition ([int]$x,[int]$y) {
# Get current cursor position and store away
$position=$host.ui.rawui.cursorposition
# Store new X and Y Co-ordinates away
$position.x=$x
$position.y=$y
# Place modified location back to $HOST
$host.ui.rawui.cursorposition=$position
} # end function set-consolePosition

Function Invoke-Rabbit
{
 $str1 = @”

^          ^
^          ^
^          ^
^          ^
   0    0
    =o=
    \!/

“@

for($i = 0 ; $i -le 15 ; $i++)
{
 
 clear-host
 set-ConsolePosition -x $x -y $y
 Write-Host -ForegroundColor $i $str1
 Start-Sleep -Milliseconds 125
 [int]$x = Get-Random -Minimum 5 -Maximum 80
 [int]$y = Get-Random -Minimum 1 -Maximum 20
}
} #end function invoke-rabbit

# *** Entry point to script ***
$Host.UI.RawUI.WindowTitle = “Happy Birthday Scripting Wife”
invoke-rabbit

I decided to use Microsoft MVP Sean Kearney’s console helper functions from last Sunday’s Weekend Scripter post to set the position for the rabbit.

The Invoke-Rabbit function begins with a here-string that draws the bunny to the Windows PowerShell console. This code is shown here:

$str1 = @”

^          ^
^          ^
^          ^
^          ^
   0    0
    =o=
      \!/

“@

The Invoke-Rabbit function loops several times, clears the Windows PowerShell console host, and then calls the Set-ConsolePosition function and passes initial coordinates. The rabbit, contained in $str1, is then written to the Windows PowerShell console in the first color (color 0). The Start-Sleep cmdlet is used to pause the script for 125 milliseconds. Next, the Get-Random cmdlet is called twice to get values for $x and for $y. These values will be passed to Set-ConsolePosition on the next time through. The script loops until the value of $i is less than or equal to 15. This portion of the script is shown here:

for($i = 0 ; $i -le 15 ; $i++)
{
 
 clear-host
 set-ConsolePosition -x $x -y $y
 Write-Host -ForegroundColor $i $str1
 Start-Sleep -Milliseconds 125
 [int]$x = Get-Random -Minimum 5 -Maximum 80
 [int]$y = Get-Random -Minimum 1 -Maximum 20
}
} #end function invoke-rabbit

The entry point to the script sets a custom title for the Windows PowerShell console, and calls the Invoke-Rabbit function. This portion of the script is shown here:

$Host.UI.RawUI.WindowTitle = “Happy Birthday Scripting Wife”
invoke-rabbit

When the script runs, output similar to that shown in the following image appears in various locations on the Windows PowerShell console.

Image of script output

We invite you to follow us on Twitter and Facebook. If you have any questions, send email to us at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow when we begin a fresh week of Hey, Scripting Guy! Blog posts. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys


0 comments

Discussion is closed.

Feedback usabilla icon