PowerShell Function to Determine Daylight Saving Time

About a year ago, I wrote a PowerShell one-liner to “Calculate What Date Thanksgiving is on with PowerShell” (in the United States). When looking back at that blog now, my first thought is boy that is some rough code that’s hard to read. Instead of re-writing the same thing again, I decided to write something similar applying what I’ve learned during the past year to it.

That’s how I came up with the idea to write a PowerShell function that returns the beginning and ending dates for daylight saving time in the USA for a given year or years:

I can’t say that I’ve seen someone accept multiple integers before, this is something I normally see with strings but it appears to work without issue.

The really cool thing is that you can use the range operator to pipe in a range of years:

dst1

You can also use the range operator to specify a range of years via parameter intput:

dst2

If you found this interesting and want to learn more about PowerShell, there’s a PowerShell Saturday technology event in Atlanta on October 26th. They have an awesome lineup of speakers scheduled to present and as the winner of the advanced category in the 2013 scripting games, I’ll be presenting a session on “Best Practices and Lessons Learned from the 2013 Scripting Games“.

The function shown in this blog can be downloaded from the TechNet script repository.

µ

Leave a Reply

%d bloggers like this: