r/PowerShell • • Dec 26 '19

Get-HappyNewYear - Yes, bored at work...

Just made a little function for fun, what do you think ?

<# Give you timeleft before NewYear
Wish you an happy NewYear if 1st january #>

Function Get-HappyNewYear {
    # Variables
    $ActualYear = (Get-Date).Year
    $NextYear = $ActualYear + 1
    $TimeLeft = New-TimeSpan -Start (Get-Date) -End ("01-01-$NextYear" -as [datetime])

    $d = $TimeLeft.Days
    $h = $TimeLeft.Hours
    $m = $TimeLeft.Minutes
    $s = $TimeLeft.Seconds
    $Totalh = [math]::Round($TimeLeft.TotalHours)

    # Job
    Write-Host "There are $d day(s), $h hour(s), $m minute(s) and $s second(s) left before the New Year !"

    if ((Get-Date).Day -eq 1 -and (Get-Date).Month -eq 1 ) {
        Write-Host "But, actually it's 1st January so... HAPPY NEW YEAR $ActualYear !!!" -ForegroundColor Red -BackgroundColor Yellow
    }
    else {
        Write-Host "So... Be patient, the new year is in $Totalh hour(s) and $m minute(s) !"
    }
}
118 Upvotes

25 comments sorted by

6

u/[deleted] Dec 26 '19

Absolutely genius.

2

u/[deleted] Dec 26 '19

Thank you !

5

u/[deleted] Dec 26 '19

For those who want something dynamic, you can just use this after :

Do {
    Clear
    Get-HappyNewYear
    sleep 1
}
While ($true)

3

u/[deleted] Dec 26 '19

I think I would prefer

While ($false)

I wouldn't want the New Year to sneak up on me...

2

u/Polyolygon Dec 26 '19

Do {
Clear
Get-HappyNewYear
sleep 1
}
While ($true)

I had to do:

    if ((Get-Date).Day -eq 1 -and (Get-Date).Month -eq 1 ) {
        Write-Host "But, actually it's 1st January so... HAPPY NEW YEAR $ActualYear !!!" -ForegroundColor Red -BackgroundColor Yellow
        $newYear = $true
    }
    else {
        Write-Host "So... Be patient, the new year is in $Totalh $hour and $m $minute !"
        $newYear = $false
    }
}

Do {
    Clear-host
    Get-HappyNewYear
    sleep 1
} Until ($newYear -eq $true)

11

u/Polyolygon Dec 26 '19

I revamped part of it to selectively do plurals for the Days, Hours, Minutes, and Seconds. Also added the repeated while loop until New years happens.

<# Give you timeleft before NewYear
Wish you an happy NewYear if 1st january #>

Function Get-HappyNewYear {
    # Variables
    $ActualYear = (Get-Date).Year
    $NextYear = $ActualYear + 1
    $TimeLeft = New-TimeSpan -Start (Get-Date) -End ("01-01-$NextYear" -as [datetime])

    $d = $TimeLeft.Days
    $m = $TimeLeft.Minutes
    $s = $TimeLeft.Seconds
    if ($d -gt 1) { $day = "days" } else { $day = "day" }
    if ($m -gt 1) { $minute = "minutes" } else { $minute = "minute" }
    if ($s -gt 1) { $second = "seconds" } else { $second = "second" }
    $Totalh = [math]::Round($TimeLeft.TotalHours)
    if ($Totalh -gt 1) { $hour = "hours" } else { $hour = "hour" }

    # Job
    Write-Host "There are $d $day, $m $minute and $s $second left before the New Year !"

    if ((Get-Date).Day -eq 1 -and (Get-Date).Month -eq 1 ) {
        Write-Host "But, actually it's 1st January so... HAPPY NEW YEAR $ActualYear !!!" -ForegroundColor Red -BackgroundColor Yellow
        $newYear = $true
    }
    else {
        Write-Host "So... Be patient, the new year is in $Totalh $hour and $m $minute !"
        $newYear = $false
    }
}

Do {
    Clear-host
    Get-HappyNewYear
    sleep 1
} Until ($newYear -eq $true)

6

u/cswimc Dec 27 '19
<# Give you timeleft before NewYear
Wish you an happy NewYear if 1st january #>

Function Get-HappyNewYear {
    # Variables
    $ActualYear = (Get-Date).Year
    $NextYear = $ActualYear + 1
    $TimeLeft = New-TimeSpan -Start (Get-Date) -End ("01-01-$NextYear" -as [datetime])

    $d = $TimeLeft.Days
    $m = $TimeLeft.Minutes
    $s = $TimeLeft.Seconds
    if ($d -gt 1) { $day = "days" } else { $day = "day" }
    if ($m -gt 1) { $minute = "minutes" } else { $minute = "minute" }
    if ($s -gt 1) { $second = "seconds" } else { $second = "second" }
    $Totalh = [math]::Round($TimeLeft.TotalHours)
    if ($Totalh -gt 1) { $hour = "hours" } else { $hour = "hour" }

    # Job
    echo "There are $d $day, $m $minute and $s $second left before the New Year !"

    if ((Get-Date).Day -eq 1 -and (Get-Date).Month -eq 1 ) {
        echo "But, actually it's 1st January so... HAPPY NEW YEAR $ActualYear !!!"
        $newYear = $true
    }
    else {
        echo "So... Be patient, the new year is in $Totalh $hour and $m $minute !"
        $newYear = $false
    }
}

$Announcement = Get-HappyNewYear
Add-Type -AssemblyName System.speech
$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer
$speak.selectvoice("Microsoft Zira Desktop")
$speak.speak($Announcement)

I slightly modded your version and made it speak. I took out the while loop though since you probably wouldn't want it to continually announce this.

7

u/ISureHateMyCat Dec 26 '19

Check out the System.Speech.Synthesis.SpeechSynthesis object and you can make it audibly wish you a “Happy New Year!”

6

u/i_hate_patrice Dec 27 '19
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$NewYearForm = New-Object System.Windows.Forms.Form
$NewYearForm.StartPosition = "CenterScreen"
$NewYearForm.Size = New-Object System.Drawing.Size(250,250)
$NewYearForm.BackColor = [System.Drawing.Color]::FromArgb(255,34,34,34)
$NewYearForm.FormBorderStyle = 'Fixed3D'
$NewYearForm.MaximizeBox = $false
$NewYearForm.Font = New-Object System.Drawing.Font("Courier New", 12)

$NewYearLabel = New-Object System.Windows.Forms.Label
$NewYearLabel.Location = New-Object System.Drawing.Size(30, 30)
$NewYearLabel.Size = New-Object System.Drawing.Size(200, 150) 
$NewYearLabel.ForeColor = "white"
$NewYearForm.Controls.Add($NewYearLabel)

[void] $NewYearForm.Show()

Function NewYearAnnouncement()
{
    $NewYearYay = "Happy New Year!!"
    Add-Type -AssemblyName System.speech
    $speak = New-Object System.Speech.Synthesis.SpeechSynthesizer
    $speak.selectvoice("Microsoft Zira Desktop")
    $speak.speak($NewYearYay)
}

Function Get-HappyNewYear {
    # Variables
    $ActualYear = (Get-Date).Year
    $NextYear = $ActualYear + 1
    $TimeLeft = New-TimeSpan -Start (Get-Date) -End ("01-01-$NextYear" -as [datetime])

    $d = $TimeLeft.Days
    $m = $TimeLeft.Minutes
    $s = $TimeLeft.Seconds
    if ($d -gt 1) { $day = "days" } else { $day = "day" }
    if ($m -gt 1) { $minute = "minutes" } else { $minute = "minute" }
    if ($s -gt 1) { $second = "seconds" } else { $second = "second" }
    $Totalh = [math]::Round($TimeLeft.TotalHours)
    if ($Totalh -gt 1) { $hour = "hours" } else { $hour = "hour" }

    # Job 
    $NewYearLabel.Text = "There are $d $day, $m $minute and $s $second left before the New Year ! `n"


    if ((Get-Date).Day -eq 1 -and (Get-Date).Month -eq 1 ) {
        $NewYearLabel.ForeColor = "red"
        $NewYearLabel.Text = "But, actually it's 1st January so... HAPPY NEW YEAR $ActualYear !!!"
        NewYearAnnouncement
        $newYear = $true
    }
    else {
        $NewYearLabel.Text += "So... Be patient, the new year is in $Totalh $hour and $m $minute !"
        $newYear = $false
    }

    $NewYearForm.Refresh()
}

Do {
    Get-HappyNewYear
    sleep 1
} Until ($newYear -eq $true)

With GUI and sound

4

u/gangculture Dec 26 '19

thanks for contributing, sorry you have to work today comrade

1

u/arnie311 Dec 31 '19

line 16 is missing hours

1

u/[deleted] Jan 02 '20

Yes, it's corrected.

Also, i made a final version bellow, i think it's better (Happy new year btw) !

<# Give you timeleft before NewYear
Wish you an happy NewYear if 1st january #>

Function Get-HappyNewYear {
    # Variables
    $ActualYear = (Get-Date).Year
    $NextYear = $ActualYear + 1
    $TimeLeft = New-TimeSpan -Start (Get-Date) -End ("01-01-$NextYear" -as [datetime])

    $d = $TimeLeft.Days
    $h = $TimeLeft.Hours
    $m = $TimeLeft.Minutes
    $s = $TimeLeft.Seconds
    $Totalh = [math]::Round($TimeLeft.TotalHours)
    if ($Totalh -gt 1) { $hour = "hours" } else { $hour = "hour" }
    if ($m -gt 1) { $minute = "minutes" } else { $minute = "minute" }

    # Job
    Write-Host "Timeleft before New Year $NextYear : `r`n$d`d : $h`h : $m`m : $s`s `r`n"

    if ((Get-Date).Day -eq 1 -and (Get-Date).Month -eq 1 ) {
        Write-Host "But, actually it's 1st January so... HAPPY NEW YEAR $ActualYear !!!" -ForegroundColor Red -BackgroundColor Yellow
    }
    else {
        Write-Host "So... Be patient, the new year is in $Totalh $hour and $m $minute !"
    }
}

Do {
    Clear-Host
    Get-HappyNewYear
    Start-Sleep 1
}
While ($true)

1

u/thalpius Dec 26 '19

Build it in PowerShell web framework 'Universal Dashboard' and make an auto refreshing counter running in Azure!

3

u/[deleted] Dec 26 '19

Great idea but I don't have enough skills and I don't have Azure... But if someone wants to do it I'm curious to see that!

1

u/[deleted] Dec 26 '19

How do I make the file for this to work? Like what file extension is it?

1

u/[deleted] Dec 26 '19

Just copy and past the raw code into a "file.ps1" and run it with powershell

1

u/[deleted] Dec 26 '19

Alright, thank you! I thought I was a .PS1 file, bit better to make sure. You know?

2

u/[deleted] Dec 27 '19

This is how you turn your function into a website with UniversalDashboard:

```

$dashboard = New-UDDashboard -Title 'Happy New Year' -Content {
New-UDElement -Tag 'div' -Endpoint {
Get-HappyNewYear
    } -AutoRefresh -RefreshInterval 1
}
$Server = Start-UDDashboard -Port 10001 -Dashboard $dashboard

```

http://g.recordit.co/Dd2YNxxeb5.gif

1

u/Lee_Dailey [grin] Dec 27 '19

howdy l33t_d0nut,

the triple-backtick/code-fence thing fails miserably on Old.Reddit ... so, if you want your code to be readable on both Old.Reddit & New.Reddit you likely otta stick with using the code block button.

it would be rather nice if the reddit devs would take the time to backport the code fence stuff to Old.Reddit ... [sigh ...]

take care,
lee

2

u/mcc85sdp Dec 26 '19
Function Get-HappyNewYear 
{   
    If ( ( Date | % { $_.Year % 2 } ) -eq 1 )
    {
        Do {

            Date | % {

                New-TimeSpan -Start $_ -End ( "01-01-$( $_.Year + 1 )" -as [ DateTime ] ) | % {

                    Write-Host ( "There are $( $_.Days    ) day(s),"    ,
                                 "$(           $_.Minutes ) minute(s)," ,  
                                 "and $(       $_.Seconds ) second(s)"  ,
                                 "left before the New Year !" -join " " ) -F 12 -B 14 
                }
            }
        }
        Until ( $_.Year % 2 -eq 1 )
        
        Write-Host "Happy New Year!!!" -F 12 -B 14
    }
}

Here's a way to do that using only a null variable, and properties. Useful to add function to the logic so that it's constantly refreshing the timer. ( I don't care for the colors, but I liked your idea. This is the manner in which I look for ways to use a lot less variables in a script.

Enjoy

3

u/IAmOperationalRisk Dec 27 '19

Pretty! I added a sleep parameter to it so it's slightly less spammy.

1

u/dataBlockerCable Dec 26 '19

Made me grin :-)

1

u/[deleted] Dec 26 '19

Upload to powershell gallery and make it a module.