Disclaimer

The views and opinions expressed on this site are mine and do not reflect those of my employer or anyone else.

Twitter: jbarsodi

#PowerShell Date Countdown Fun

The other day I was trying to figure out how much longer until my anniversary date at work.  I figured I could open my Outlook calendar and start counting, but then I said, I bet this is easier to do in PowerShell!  So I goofed around and came up with the following.  I’m sure you could add a few more lines and loop through it to build a countdown script…

Here’s an example of how to use it.  Let’s say my anniversary date was October 1, 2010 or “10/01/10″ for this example.

First we want to figure out what today is using the Get-Date cmdlet.  Then we want to define our anniversary date and then take the difference of the two.

$today = Get-Date
$anniversary = "10/01/10"
[datetime]$anniversary - $today

This will generate output like similar to this:

Days              : 59
Hours             : 11
Minutes           : 30
Seconds           : 49
Milliseconds      : 80
Ticks             : 51390490805093
TotalDays         : 59.4797347281169
TotalHours        : 1427.51363347481
TotalMinutes      : 85650.8180084883
TotalSeconds      : 5139049.0805093
TotalMilliseconds : 5139049080.5093
While that might produce you what you want with a lot of information you don’t, you can simply use the Days property to output the number of days remaining.
$today = Get-Date
$anniversary = "10/01/10"
([datetime]$anniversary - $today).days
Which will return:
PS C:\> ([Datetime]$anniversary – $Today).days
59
Pretty simple.