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"


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 {

    Determines the last day of the specified month(s).

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

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


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

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

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



    Author:  Mike F Robbins
    Twitter: @mikefrobbins

    param (
        [datetime[]]$Date = (Get-Date).AddMonths(-1)

        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


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

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


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

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


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


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)


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)


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)


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