It's that time of year again where PowerShell can make all your wishes come true. Ok, maybe that's a bit much, but PowerShell is the gift that keeps giving all year long. Again, maybe too much. How about this? Here's a revised version of my Christmas countdown prompt. I've posted this in the past. But some of you are new to PowerShell and may have missed it. Plus I've revised it a bit.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
In PowerShell you have a built-in function called Prompt. It controls what you see in the console which is usually something like PS C:\>. But you can create your own Prompt function. Here's my Christmas countdown prompt.
Function Prompt { #get current year $year = (Get-Date).year #get a timespan between Christmas for this year and now $time=[datetime]"25 December $year" - (Get-Date) #turn the timespan into a string and strip off the milliseconds $timestring = $time.ToString().Substring(0,11) #get random string of decorative characters $front = -join (14,15,42 | Get-Random -Count 2 | Foreach { $_ -as [char] }) $back = -join (14,15,42 | Get-Random -Count 2 | Foreach { $_ -as [char] }) $text="[{0}Christmas in {1}{2}]" -f $front,$timestring,$back #get each character in the text and randomly assign each a color $text.tocharArray() | foreach { $i = get-random -Minimum 1 -Maximum 20 switch ($i) { {$i -le 20 -and $i -gt 15} { $color = "Red"} {$i -le 16 -and $i -gt 10} { $color = "Green" } {$i -le 10 -and $i -gt 5}{ $color = "DarkGreen"} default {$color = "White"} } #write each colorized character write-host $_ -nonewline -foregroundcolor $color } #foreach #the function needs to write something to the pipeline Write (" PS " + (Get-Location) + "> ") } #end function
I've inserted plenty of comments so you should be able to understand how it works. In a nutshell, the prompt inserts a message that indicates how much time remains before December 25th of the current year. The text also includes some special characters to help embellish it and put you in the holiday mood. Finally, each character is written to the host with a random color. You can see that I used a Switch construct to evaluate the random number with an expression.
This function will work best in the PowerShell console. If you want to try it out you can copy and paste this function into your console session. Your PowerShell session will now look like this:
The prompt will only last for as long as your PowerShell session is running. The next time you start PowerShell you will be back to your original prompt. If you enjoy this, put my Christmas prompt function in your PowerShell profile, but be sure it is called Prompt. When the holidays are over simply comment out the function. Or you could add some logic to your profile.
if ((Get-Date).Month -eq 12) { #load the Christmas prompt Function Prompt { ... } #close prompt } #if December
Ho-Ho-Ho! Have a great weekend.