{"id":17001,"date":"2010-09-26T00:01:00","date_gmt":"2010-09-26T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/09\/26\/modify-the-powershell-console-title-and-display-a-rabbit\/"},"modified":"2010-09-26T00:01:00","modified_gmt":"2010-09-26T00:01:00","slug":"modify-the-powershell-console-title-and-display-a-rabbit","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/modify-the-powershell-console-title-and-display-a-rabbit\/","title":{"rendered":"Modify the PowerShell Console Title and Display a Rabbit"},"content":{"rendered":"<p><strong>Summary<\/strong>: Modify the Windows PowerShell console title and display a rabbit at random locations. The Microsoft Scripting Guys have fun.<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. I love Microsoft Outlook 2010.<\/p>\n<blockquote>\n<p><a href=\"http:\/\/www.ftc.gov\/opa\/2009\/10\/endortest.shtm\">United States FTC disclaimer:<\/a> 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.<\/p>\n<\/blockquote>\n<p>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.<\/p>\n<p style=\"padding-left: 30px\"><strong>Set-Rabbit.ps1<\/strong><\/p>\n<p style=\"padding-left: 30px\"><span>function global:set-ConsolePosition ([int]$x,[int]$y) {<br \/> # Get current cursor position and store away<br \/> $position=$host.ui.rawui.cursorposition<br \/> # Store new X and Y Co-ordinates away<br \/> $position.x=$x<br \/> $position.y=$y<br \/> # Place modified location back to $HOST<br \/> $host.ui.rawui.cursorposition=$position<br \/> } # end function set-consolePosition<\/p>\n<p> Function Invoke-Rabbit<br \/> {<br \/> <span>&nbsp;<\/span>$str1 = @&#8221;<\/p>\n<p> ^<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;<\/span>^<br \/> ^<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>^<br \/> ^<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>^<br \/> ^<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>^<br \/> <span>&nbsp;&nbsp; <\/span>0<span>&nbsp;&nbsp;&nbsp; <\/span>0<br \/> <span>&nbsp;&nbsp;&nbsp; <\/span>=o=<br \/> <span>&nbsp;&nbsp;&nbsp; <\/span>\\!\/<\/p>\n<p> &#8220;@<\/p>\n<p> for($i = 0 ; $i -le 15 ; $i++)<br \/> { <br \/> <span>&nbsp;<\/span><br \/> <span>&nbsp;<\/span>clear-host<br \/> <span>&nbsp;<\/span>set-ConsolePosition -x $x -y $y<br \/> <span>&nbsp;<\/span>Write-Host -ForegroundColor $i $str1 <br \/> <span>&nbsp;<\/span>Start-Sleep -Milliseconds 125<br \/> <span>&nbsp;<\/span>[int]$x = Get-Random -Minimum 5 -Maximum 80<br \/> <span>&nbsp;<\/span>[int]$y = Get-Random -Minimum 1 -Maximum 20<br \/> }<br \/> } #end function invoke-rabbit<\/p>\n<p> # *** Entry point to script ***<br \/> $Host.UI.RawUI.WindowTitle = &#8220;Happy Birthday Scripting Wife&#8221;<br \/> invoke-rabbit<\/span><\/p>\n<p>I decided to use Microsoft MVP Sean Kearney&rsquo;s console helper functions from <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/09\/19\/draw-boxes-and-lines-in-the-windows-powershell-console-host.aspx\">last Sunday&rsquo;s Weekend Scripter post<\/a> to set the position for the rabbit.<\/p>\n<p>The <strong>Invoke-Rabbit<\/strong> function begins with a <strong>here-string<\/strong> that draws the bunny to the Windows PowerShell console. This code is shown here:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\"><span>$str1 = @&#8221;<\/p>\n<p> ^<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>^<br \/> ^<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>^<br \/> ^<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>^<br \/> ^<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>^<br \/> <span>&nbsp;&nbsp; <\/span>0<span>&nbsp;&nbsp;&nbsp; <\/span>0<br \/> <span>&nbsp;&nbsp;&nbsp; <\/span>=o=<br \/> <span>&nbsp;&nbsp; &nbsp; &nbsp;<\/span>\\!\/<\/p>\n<p> &#8220;@<\/p>\n<p> <\/span><\/p>\n<p>The <strong>Invoke-Rabbit<\/strong> function loops several times, clears the Windows PowerShell console host, and then calls the <strong>Set-ConsolePosition<\/strong> function and passes initial coordinates. The rabbit, contained in <strong>$str1<\/strong>, is then written to the Windows PowerShell console in the first color (color <strong>0<\/strong>). The <strong>Start-Sleep<\/strong> cmdlet is used to pause the script for 125 milliseconds. Next, the <strong>Get-Random<\/strong> cmdlet is called twice to get values for <strong>$x<\/strong> and for <strong>$y<\/strong>. These values will be passed to <strong>Set-ConsolePosition<\/strong> on the next time through. The script loops until the value of <strong>$i<\/strong> is less than or equal to 15. This portion of the script is shown here:<\/p>\n<p style=\"padding-left: 30px\"><span>for($i = 0 ; $i -le 15 ; $i++)<br \/> { <br \/> <span>&nbsp;<\/span><br \/> <span>&nbsp;<\/span>clear-host<br \/> <span>&nbsp;<\/span>set-ConsolePosition -x $x -y $y<br \/> <span>&nbsp;<\/span>Write-Host -ForegroundColor $i $str1 <br \/> <span>&nbsp;<\/span>Start-Sleep -Milliseconds 125<br \/> <span>&nbsp;<\/span>[int]$x = Get-Random -Minimum 5 -Maximum 80<br \/> <span>&nbsp;<\/span>[int]$y = Get-Random -Minimum 1 -Maximum 20<br \/> }<br \/> } #end function invoke-rabbit<\/span><\/p>\n<p>The entry point to the script sets a custom title for the Windows PowerShell console, and calls the <strong>Invoke-Rabbit<\/strong> function. This portion of the script is shown here:<\/p>\n<p style=\"padding-left: 30px\"><span>$Host.UI.RawUI.WindowTitle = &#8220;Happy Birthday Scripting Wife&#8221;<br \/> invoke-rabbit<\/span><\/p>\n<p>When the script runs, output similar to that shown in the following image appears in various locations on the Windows PowerShell console.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1680.WES09261001_6DD52B37.jpg\"><img decoding=\"async\" width=\"554\" height=\"323\" title=\"Image of script output\" style=\"border: 0px\" alt=\"Image of script output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5226.WES09261001_thumb_2245D7B3.jpg\" border=\"0\" \/><\/a><\/p>\n<p>We invite you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow when we begin a fresh week of Hey, Scripting Guy! Blog posts. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/p>\n<p><b><br \/><\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25,3,4,61,45],"class_list":["post-17001","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-displaying-output","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17001","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17001"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17001\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=17001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}