PowerShell: Get the Last Day of the Previous Month

While working with dates in PowerShell recently, I was required to get the last day of the previous month. In this tutorial, I will explain different methods to get the last day of the previous month using PowerShell with examples.

To get the last day of the previous month in PowerShell, you can use the AddMonths and AddDays methods. First, obtain the current date using Get-Date, then calculate the first day of the current month with $currentDate.AddDays(1 - $currentDate.Day). Finally, subtract one day from the first day of the current month to get the last day of the previous month using $firstDayOfCurrentMonth.AddDays(-1).

Get the Last Day of the Previous Month in PowerShell

There are different methods to get the last day of the previous month in PowerShell. Let me show you all these methods with examples.

Method 1: Using AddMonths and AddDays

One of the simplest ways to get the last day of the previous month in PowerShell is to use the AddMonths and AddDays methods. Here’s how you can do it:

# Get the current date
$currentDate = Get-Date

# Get the first day of the current month
$firstDayOfCurrentMonth = $currentDate.AddDays(1 - $currentDate.Day)

# Subtract one day to get the last day of the previous month
$lastDayOfPreviousMonth = $firstDayOfCurrentMonth.AddDays(-1)

# Output the result
$lastDayOfPreviousMonth

In this example, we first obtain the current date using Get-Date. We then calculate the first day of the current month by subtracting the current day minus one from the current date. Finally, we subtract one day from the first day of the current month to get the last day of the previous month.

Here is the output you can see in the screenshot below:

powershell get last day of previous month

Check out Get Day of Month in PowerShell

Method 2: Using Get-Date with AddMonths

Another method to get the last day of the previous month in PowerShell involves directly manipulating the date using Get-Date and AddMonths.

Here is an example.

# Get the last day of the previous month
$lastDayOfPreviousMonth = (Get-Date -Day 1).AddMonths(-1).AddDays((Get-Date -Day 1).AddMonths(1).AddDays(-1).Day - 1)

# Output the result
$lastDayOfPreviousMonth

This approach uses the Get-Date cmdlet to set the day to the first of the month, then adjusts the month using AddMonths, and finally calculates the last day by subtracting the necessary days.

Here is the output in the screenshot below; it is giving the exact output:

get last day of previous month in powershell

Read Get the Last Business Day of the Month Using PowerShell

Method 3: Custom Function

For more reusable code, you can create a custom function to determine the last day of the previous month:

function Get-LastDayOfPreviousMonth {
    # Get the current date
    $currentDate = Get-Date

    # Calculate the first day of the current month
    $firstDayOfCurrentMonth = $currentDate.AddDays(1 - $currentDate.Day)

    # Subtract one day to get the last day of the previous month
    $lastDayOfPreviousMonth = $firstDayOfCurrentMonth.AddDays(-1)

    # Return the result
    return $lastDayOfPreviousMonth
}

# Call the function
Get-LastDayOfPreviousMonth

This function encapsulates the logic in a reusable format, making it easy to call whenever you need the last day of the previous month.

You can see the exact output in the screenshot below:

How to get last day of previous month in powershell

Conclusion

In this tutorial, I have explained how to get the last day of the previous month in PowerShell using the AddMonths and AddDays methods, directly manipulate the date with Get-Date, or create a custom function, etc. I hope this helps.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.