Powershell – Oh Christmas Tree, Oh Christmas tree – One more change

| | Comments (0)


Last time we updated the Tree script from BSONPOSH and www.get-admin.com

This time I took my version and went; well nuts.

I decided to try and make the lights “twinkle” by building an array of “Light locations” and selecting randomly those targets to be on or off.   I also decided to have two new characters be the twinkly lights.   And more colours.

And for fun?  A Marquee.

So what we have is a two fold solution.

A nice sizeable tree in Powershell, and errr a great burn in utility.

You see, it’s coded really badly and eats CPU :)




Enjoy and Happy Holidays

The Energized Tech


# PowershellTree-V-whatever.PS1

#Clear the Screen


#Move it all down the line


# Assign two special characters into an array for potential "LIGHTS"

$starchar=[char][byte]15, "*"

# Number of rows Deep for the Tree- from 2 to whatever fits on the screen, after 30 it gets funky

$Rows = 30

# These variables are for the Bouncing Marquee at the bottom
# Column, number of Columns to move (relative to size of tree)
# and Direction it will move


# Standard console Colours
# Just for fun I added in all the possible Console Foreground Colors
# Delete whichever ones you don't like

$colors = "DarkRed","DarkBlue","DarkCyan","DarkMagenta","DarkYellow","Gray","DarkGray","Blue","Green","Cyan","Red","Magenta","Yellow","White"

# Get where the Cursor was

$oldpos = $host.ui.RawUI.CursorPosition

# BsponPosh’s ORIGINAL Tree building Algorithm :)
# None of this would be possible if it weren’t for him

    Foreach ($r in ($rows..1)){
        write-host $(" " * $r) -NoNewline
        1..((($rows -$r) * 2)+1) | %{
                write-Host "*" -ForegroundColor Darkgreen  -nonewline
        write-host ""

    # trunk

# A slight change, an extra row on the stump of the tree
# and Red (Trying to make it look like a brown trunk

    write-host $("{0}***" -f (' ' * ($Rows -1) ))  -ForegroundColor DarkRed
    write-host $("{0}***" -f (' ' * ($Rows -1) ))  -ForegroundColor DarkRed
    write-host $("{0}***" -f (' ' * ($Rows -1) ))  -ForegroundColor DarkRed

$host.ui.RawUI.CursorPosition = $oldpos

# New Addins by Sean “The Energized Tech” Kearney

# Compute the possible number of stars in tree (Number of Rows Squared)


# Number of lights to give to tree.  %25 percent of the number of green stars.  You pick

$numberlights=$numberstars *.35

# Initialize an array to remember all the “Star Locations”

for ($i = 0; $i -lt $numberlights; $i++)

# Probably redundant, but just in case, remember where the  heck I am!

$oldpos = $host.ui.RawUI.CursorPosition

# New change.  Create an Array of positions to place lights on and off

foreach ($light in ($numberlights..1))
    # Pick a Random Row

    $row=(get-random -min 1 -max (($Rows)+1))

    # Pick a Random Column – Note The Column Position is
    # Relative to the Row vs Number of Rows

    $column=($Rows-$row)+(get-random -min 1 -max ($row*2))

    #Grab the current position and store that away in a $Temp Variable

    # Now Build new location of X,Y into $HOST

    # Store this away for later

    # Now update that “STAR” with a Colour
# Repeat this OVER and OVER and OVER and OVER



# Now we just pull all those stars up and blank em back
# on or off randomly 7 at a time

for ($light=1; $light -lt 7; $light++)
    # Set cursor to random location within Predefined "Star Location Array"

    $host.ui.RawUI.CursorPosition=($Starlocation | get-random)
    # Pick a random number between 1 and 1000
    # if 500 or higher, turn it to a light
    # Else turn it off

    $flip=get-random -min 1 -max 1000
    if ($flip -gt 500)

        # Write a Random "star character" on the screen
    # in a Random Foreground Color from defined sets

    write-Host ($starchar | get-random) -ForegroundColor  ($colors | get-random) -nonewline
    write-host "*" -Foregroundcolor DarkGreen -nonewline

# Remember where we are


# Set a position for the row and Column


# update the console


# Bump up the column position based upon direction


# Ok this was a BAD way to do it but it works for
# Christmas.   If we hit the right side change
# Direction to backwards.  If we hit the left side
# Change direction forwards

If ($BottomColumn -gt $Rows)
{ $Direction=-1 }

If ($BottomColumn -lt 1)
{ $Direction=1 }

# Print greeting.  Space must be before and after to avoid
# Trails.  Output in Random Colour

write-host " Happy Holidays Powershell " -ForegroundColor  ($colors | get-random)

# End of the loop, keep doin’ in and go “loopy!”


Leave a comment