Some days the week just seems to drag on and on and on….Like many of you, I can’t wait for the end of the work day on Friday afternoon. But how much longer is it until I can say, in a cliche ridden fashion, “TGIF!” Fortunately I have PowerShell to give me a pretty accurate answer.

I wote a simple script, Get-TGIF.ps1, which lets me know how much longer I have to wait. Or if the weekend has started, that I’d better get going.

#get Friday 5:00PM for the current week

$now=Get-Date

[datetime]$TGIF="{0:MM/dd/yyyy} 5:00:00 PM" -f (($now.AddDays( 5 - [int]$now.DayOfWeek)) )

if ((get-date) -ge $TGIF) {

 write-host "TGIF has started without you!" -fore Green

 }

else {

   write-host "Countdown: $( ($tgif -(get-date)).ToString()  )" -fore magenta

 }

 #end script

The script is an example of a countdown. It gets the current date and time and saves it to $now. Then it creates a timespan object subtracting the current date and time from 5:00PM on Friday for the current week. But how do I know when that will be?

In order to properly define the $TGIF variable, I need to get the date for the next Friday. I can do this by using the integer value of the DayOfWeek property which normally just gives you the day.

PS C:\> (get-date).DayOfWeek
Thursday

However, each value has an underlying integer value.

PS C:\> (get-date).DayOfWeek -as [int]
4

Depending on your culture or regional settings you might have a different value. Usually Monday is day 1 which means Friday is 5. Therefore I simply have to subtract the current day value from 5 and then I’ll know the date of the current week’s Friday.

If the current time is greater than $TGIF then I know it’s after 5:00PM on Friday and I should be doing something else. Otherwise, the script writes a message to the host displaying the timespan object as a string.  I even created an alias, tgif, for the script so I can easily see how much longer I have.

Certainly you can change the deadline to whatever time you prefer and customize the messages.

4 Responses to “Is It Friday Yet?”

  1. Hah! Cool and fun profile script add-on. I tossed this onto the end of my profile script so it pops out when I open up PowerShell (though, your alias idea is probably better since the console tends to be open all day).

    I did tweak the output a bit to make it more readable…

    For your “else” statement:

    else {
    $diff = $($tgif -(get-date))
    write-host “Countdown to TGIF: $($diff.Days)d $($diff.Hours)h $($diff.Minutes)m” -fore red
    }

    This outputs a bit cleaner output:

    Countdown to TGIF: 1d 1h 39m

    Example on screen: http://bit.ly/3Lg6VM

    • Jeffery Hicks
      16:03, 05.11.2009

      I like that change. The script started out “quick and dirty” and never moved much beyond it.

    • Jeffery Hicks
      16:10, 05.11.2009

      Another fun enhancement would be to use a different for the countdown method depending on how many days or hours are remaining. Maybe moving from red to green the closer you get. Heck, I might even throw in the voice object .

  2. >>Heck, I might even throw in the voice object .

    HAH! Something that reads off the $host and if your PowerShell window is still open past closing time–”warning” you via voice API to go home. I like it! :)

    I’m posting up a quick post in a bit… got to thinking and since I tend to leave my PS window open all day, I added it to the window bar (or tab, since I use Console2). More on that in a few.