Two PowerShell Functions for the Christmas Season


Last night I spoke at DAYSPUG in Dayton Ohio, and since Christmas is only about 10 days away – I thought it would be fun to introduce two Christmas-related PowerShell Functions that I threw together.

The two functions are:

  1. New-ChristmasTree – This function generates an awesome, and mostly tacky animated, flashing Christmas tree in the PowerShell console.
  2. Get-DaysUntilChristmas – This function uses New-TimeSpan and Get-Date to calculate the number of days until Christmas.  The output is something like “There are 10 days until Christmas!”

Here is the New-ChristmasTree function:

function New-ChristmasTree {
[array]$Colors = "DarkRed","Green","Red","White"
$oldBG = $Host.UI.RawUI.BackgroundColor
$Host.UI.RawUI.BackgroundColor = "Black"
function New-Tree {
Clear-Host
$Color = $Colors | Get-Random
Write-Host "                         O                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                        OOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                       OOOOO                   " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                      OOOOOOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                     OOOOOOOOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                    OOOOOOOOOOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                   OOOOOOOOOOOOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                  OOOOOOOOOOOOOOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                 OOOOOOOOOOOOOOOOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                OOOOOOOOOOOOOOOOOOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "               OOOOOOOOOOOOOOOOOOOOO                     " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "             OOOOOOOOOOOOOOOOOOOOOOOOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "            OOOOOOOOOOOOOOOOOOOOOOOOOOO                    " -ForegroundColor $Color
Start-Sleep -Seconds 1
}
New-Tree
New-Tree
New-Tree
New-Tree
New-Tree
New-Tree
New-Tree
New-Tree
New-Tree
New-Tree
$Host.UI.RawUI.BackgroundColor = $oldBG
Clear-Host
}

Here is the Get-DaysUntilChristmas function:

function Get-DaysUntilChristmas {
$Christmas = Get-Date 12/25
$Today = Get-Date
$TimeSpan = New-TimeSpan -Start $Today -End $Christmas
$Days = $TimeSpan.Days
Write-Host "There are $Days days until Christmas!"
}

Here is a screenshot of the Get-DaysUntilChristmas function in action:

Advertisements

3 thoughts on “Two PowerShell Functions for the Christmas Season

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s