Pluralsight blog Where devs, IT admins & creative pros go for news, tips, videos and more.
Pluralsight + Digital-Tutors - 3,000 tech & creative courses - starting at $29/month Get it now →
June 28, 2013

Jeff Hicks’ PowerShell Challenge: Friday fun!

By powershellbig

All week we’ve been collecting your PowerShell scripts that accomplish some unusual or off-the-wall task. The contest is winding down and time is running out for you to submit your wacky and wonderful PowerShell examples. Content details are at the end of this post.

I’m going to wrap up this week with an end-of-the-year PowerShell script that has absolutely no redeeming production value but sure is fun. This is a script I call Get-HappyHolidays.ps1. Although perhaps a more accurate verb would be Invoke or New. You can use whatever curls your ribbon.

#requires -version 2.0 <# This must be run from the PowerShell console. NOT in the ISE. This is based on a script originally posted by The PowerShell Guy http://thepowershellguy.com/blogs/posh/default.aspx #> Param( [string]$Caption = "Happy Holidays" ) #add the caption to the greeting line $Greeting = "$([char]14) **$Caption** $([char]14)" Clear-Host Write-host "`n" $Peek = " ^ " $tree = "/|\" $i = 20 $pos = $host.ui.rawui.CursorPosition #adjust to center the display $offset = ($host.ui.rawui.WindowSize.Width - 72)/2 Write-Host -ForegroundColor Red ($peek.PadLeft($i-1).PadRight(36) * 2) Write-Host -ForegroundColor Green ($tree.PadLeft($i-1).PadRight(36) * 2) 1..16 | Foreach { #build out the tree $tree = $tree -replace "/(.*)\\",'//$1\\' Write-Host -ForegroundColor Green ($tree.PadLeft($i).PadRight(36) * 2) $i++ } Write-Host -ForegroundColor Green ("|||".PadLeft(19).PadRight(36) *2) Write-Host -ForegroundColor Green ("|||".PadLeft(19).PadRight(36) *2) $rect = New-Object System.Management.Automation.Host.Rectangle $rect.top = $pos.y $rect.Right = 70 $rect.Bottom = $pos.y + 19 $buffer = $host.ui.rawui.getbuffercontents($rect) #random number object $R = New-Object System.Random $ball = New-Object System.Management.Automation.Host.BufferCell $ball.backgroundColor = $host.ui.rawui.BackgroundColor 1..150 | ForEach { #pause for a random number of milliseconds between 50 and 200 sleep -Milliseconds (Get-Random -Minimum 50 -Maximum 200) #get a random position $rx = $r.Next(19) $ry = $r.Next(70) #define a collection of figures to be used as ornaments $ornaments = '@','*','#',":","$" #get a random ornament $ball.Character = Get-Random $ornaments $ball.ForegroundColor = $r.next(16) if ($buffer[$rx,$ry].Character -eq '/') {$buffer[$rx,$ry] = $ball} if ($buffer[$rx,$ry].Character -eq '\') {$buffer[$rx,$ry] = $ball} $host.ui.rawui.SetBufferContents($pos,$buffer) } #write the greeting centered $pos.y = $pos.y + 22 $pos.x = 36 - (($Greeting.Length)/2) $host.ui.rawui.CursorPosition = $pos Write-Host $Greeting -ForegroundColor Green -BackgroundColor Red #add a couple blank lines Write-Host "`n`n"

This script is a re-working of something posted a number of years ago by MoW, the PowerShell Guy. I wanted to add some variety and some flexibility in captioning. You can probably figure out what the PowerShell code will do. In fact, I don’t want to spoil the fun so I’m not going to include a screen shot. Save the code to your computer and run it in a PowerShell session. It will work in PowerShell 2.0 or 3.0. It will probably even work in 1.0, but I don’t have that running anymore to verify and hopefully you don’t either. It won’t run in the PowerShell ISE.

If you haven’t shared your wacky PowerShell code yet, I hope you will. You never know, someone else might need it or perhaps your script will illustrate a PowerShell concept or cmdlet and teach something new. I hope you’ve enjoyed this week. If so, I hope you’ll follow my blog. I run a series called Friday Fun that posts PowerShell examples doing something fun but hopefully is educational as well.

Remember, you still have until Sunday at midnight CST to submit your wacky PowerShell code.Submit the most creative ways you’ve used PowerShell by completing the form below. To provide you with further inspiration, I’ll post another example on Friday of this week on the TrainSignal blog. Entries will be accepted through midnight CST on Sunday, June 30, and you can enter as many times as you’d like. One randomly selected grand-prize winner will receive a one-year subscription to TrainSignal, a $468 value. A runner-up will receive a free month of training. We’ll post some of the entries on the blog so everyone can check out your handiwork.

Fill out my online form.

By submitting this form, you will be opting in to TrainSignal’s mailing list. We will never sell your contact information to other companies, and you can opt out at any time.

TrainSignal trial

About the Author

is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for MPCMag.com, is a regular contributor to the Petri IT Knowledgebase, 4SysOps and the Altaro Hyper-V blog, as well as frequent speaker at technology conferences and user groups. If he isn't writing, Jeff is most likely recording training videos for companies like TrainSignal or hanging out in the forums at PowerShell.org.

Author's Website: http://jdhitsolutions.com/blog/


Discussion