Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun Christmas Countdown Prompt

Posted on December 5, 2014

christmaslights 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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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:

christmasprompt2014

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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d