In last week's Friday Fun post, I shared with you a PowerShell prompt that would display a festive Christmas countdown clock. This week I have another holiday related prompt function. This one is pretty straight forward and is not that much different from the default PowerShell prompt function.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Function Prompt { #only change background color if in December if ((Get-Date).Month -eq 12 -And (Get-Date).Day -lt 25) { Switch ($host.ui.RawUI.BackgroundColor) { "DarkGreen" { $c = "DarkRed" } "DarkRed" { $c = "DarkGreen" } Default { #get a random color $c = Get-Random -InputObject "DarkRed","DarkGreen" } } #switch $host.ui.RawUI.BackgroundColor = $c } "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " }
I included some logic so that my customization only happens during the month of December and before Christmas. The prompt changes the background color of your console between DarkRed and DarkGreen. The first time you run it, the prompt will randomly select a color. You might want to run Clear-Host, or CLS after loading the prompt.
After that the background color will toggle leaving you with an effect like this:
It is interesting to see how different commands write to the console. You could even combine both of my prompts if you are in an especially festive mood.
Function Prompt { #only change background color if in December if ((Get-Date).Month -eq 12 -And (Get-Date).Day -lt 25) { Switch ($host.ui.RawUI.BackgroundColor) { "DarkGreen" { $c = "DarkRed" } "DarkRed" { $c = "DarkGreen" } Default { #get a random color $c = Get-Random -InputObject "DarkRed","DarkGreen" } } #switch $host.ui.RawUI.BackgroundColor = $c #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 } #if Christmas time "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) " } #end prompt function
There's probably no practical value in this other than having some fun and maybe understanding some PowerShell scripting concepts. Hope your holiday shopping is going well. Have a great weekend.