PowerShell Calculate First and Last Day in a Month

Today I was writing a script to rotate some log files on a machine, as for regulatory reasons I need to keep logs for a number of years I thought to archive files on a monthly basis so to make archival easier.

My main goal is not locking myself into a script that would simply remove/archive all files from a certain date onward, for example get first day of previous month and archive anything newer than that, but rather something that would let PowerShell calculate first and last day in a month and then archive any log file created in that timeframe.

Calculate the first day of a given month it’s easy as PowerShell does this natively for example

$firstDayOfMonth = (Get-Date -Year 2016 -Month 5 -Day 1)
Sunday, May 01, 2016 6:18:00 AM

Of course you can tweak the above command to format output a bit for example

Get-Date -Year 2016 -Month 5 -Hour 00 -Minute 00 -Second 00

Friday, May 20, 2016 12:00:00 AM

Calculating the last day is not that straightforward but thankfully native .NET can help here

$previousMonth= (Get-Date).AddMonths(-1)
$lastDayOfMonth = [System.DateTime]::DaysInMonth($previousMonth.Year, $previousMonth.Month)
[datetime]"$($previousMonth.Month), $lastDayOfMonth, $($previousMonth.Year), 23:59:59.999"

# Output
sabato, 30 aprile 2016 23:59:59

The above will print directly to screen result of the last month’s day but you can easily change it so to save result to a variable for example

$tmpLastDayOfMonth = [System.DateTime]::DaysInMonth($archiveMonth.Year, $archiveMonth.Month)

# Format returned value of the .NET [System.DateTime]::DaysInMonth to a proper datetime
$lastDayOfMonth = [datetime]"$($archiveMonth.Month), $tmpLastDayOfMonth, $($archiveMonth.Year), 23:59:59.999"

If you are simply trying to find last day of the current month this is easier and can be accomplished like this

Get-Date -day 1 -hour 0 -minute 0 -second 0).AddMonths(1).addseconds(-1)

Tuesday, May 31, 2016 11:59:59 PM

I’m sure there are other, perhaps better, ways to achieve the same result and I’d be curious to read your comments below with your suggestion and experiences.

[su_note note_color=”#ffff96″ text_color=”#000000″ radius=”5″]Note: Please note the above output is taken from a server with en-US regional settings, the output you will get depends on the machine regional settings. In any case when using the above in a script you don’t have to worry about the date conversion, for example when searching for files older than a certain date, as PowerShell will perform the conversion internally. [/su_note]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s