Determine the Last Day of the Previous Month with PowerShell

I recently had a need to determine the last day of the previous month with PowerShell. Unless I’m overlooking something, PowerShell doesn’t natively have a way to determine this.

Luckily, the System.Time class in the .NET Framework has a DaysInMonth method that returns the number of days in a specific month which effectively gives you the last day of the month. The command to determine what I needed was simple enough:

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

lastdayofmonth0.jpg

I went ahead and created a well documented and reusable function that is more dynamic to use in similar scenarios in the future:

function Get-MrLastDayOfMonth {

<#
.SYNOPSIS
    Determines the last day of the specified month(s).

.DESCRIPTION
    Get-MrLastDayOfMonth is a function that returns the last day of one or
    more months that are specified via parameter or pipeline input.

.PARAMETER Date
    The date(s) to determine the last day for. The default is the previous month.

.EXAMPLE
    Get-MrLastDayOfMonth

.EXAMPLE
     Get-MrLastDayOfMonth -Date (Get-Date).AddMonths(1)

.EXAMPLE
     (Get-Date).AddMonths(-3) | Get-MrLastDayOfMonth

.EXAMPLE
    6..1 | Foreach-Object {(Get-Date).AddMonths(-$_)} | Get-MrLastDayOfMonth

.INPUTS
    DateTime

.OUTPUTS
    DateTime

.NOTES
    Author:  Mike F Robbins
    Website: http://mikefrobbins.com
    Twitter: @mikefrobbins
#>

    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline=$true)]
        [ValidateNotNullOrEmpty()]
        [datetime[]]$Date = (Get-Date).AddMonths(-1)
    )

    PROCESS {
        foreach ($d in $Date) {
            $LastDay = [System.DateTime]::DaysInMonth($d.Year, $d.Month)
            [datetime]"$($d.Month), $LastDay, $($d.Year), 23:59:59.999"
        }
    }

}

I’ll work through the examples of the function so you can see how it can be used. Running it without any parameters causes it to default to finding the last day of the previous month:

. .\Get-MrLastDayOfMonth.ps1
Get-MrLastDayOfMonth

lastdayofmonth1.jpg

The second example shows providing parameter input to get the last day for next month:

Get-MrLastDayOfMonth -Date (Get-Date).AddMonths(1)

lastdayofmonth2.jpg

The third example shows determining the last day of the month, three months ago:

(Get-Date).AddMonths(-3) | Get-MrLastDayOfMonth

lastdayofmonth3.jpg

And the final example shows determining the last day of the month for the previous six months via pipeline input:

6..1 | Foreach-Object {(Get-Date).AddMonths(-$_)} | Get-MrLastDayOfMonth

lastdayofmonth4.jpg

I actually figured out another way to accomplish this task in PowerShell between the time I wrote this blog and the time I published it, but I thought I would leave the previous content intact as well.

You know the first of a month is always going to be number 1 so you can actually subtract one millisecond from the first to receive the last day of the month:

(Get-Date -Date 6/1/14).AddMilliseconds(-1)

lastdayofmonth5.jpg

And for a more dynamic version of the previous one-liner, you could do something crazy like this:

(Get-Date | ForEach-Object {Get-Date -Date ([datetime]"$($_.Month) 1, $($_.Year)")}).AddMilliseconds(-1)

lastdayofmonth6.jpg

I don’t normally publish commands with aliases on my blog, but since that previous command is fairly cryptic already, I’ll make an exception in this scenario:

(date|%{date("$($_.Month) 1,$($_.Year)")}).AddMilliseconds(-1)

lastdayofmonth7.jpg

Whatever you do, please don’t use aliases or positional parameters in your scripts!

ยต