How Many Days are in This Month, Anyways?

Sometimes finding the amount of time from now isn't exactly what you need when modifying time objects in PowerShell. Sometimes, you may want to figure out how many days are in this month as a whole. 

Now, you could simply create an array in your $profile and define $Jan = 2, $Feb = 28...or 29.. ugh. No, that won't work...

How about we have the Get-Date cmdlet do the work for us.

$DaysInMonth = ((get-date -Day 1 -Hour 0 -Minute 0 -Second 0).AddMonths(1).AddSeconds(-1)).Day

A big thanks to alecxe over on StackOverflow for this tidbit of awesome! He helped out on this question and I ran across it whilst working on my little bit of Exchange Report Automation we talked about earlier.


Popular Posts