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 : 59Hours : 11Minutes : 30Seconds : 49Milliseconds : 80Ticks : 51390490805093TotalDays : 59.4797347281169TotalHours : 1427.51363347481TotalMinutes : 85650.8180084883TotalSeconds : 5139049.0805093TotalMilliseconds : 5139049080.5093
$today = Get-Date $anniversary = "10/01/10" ([datetime]$anniversary - $today).days
PS C:\> ([Datetime]$anniversary – $Today).days59
