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
$lastDayOfPreviousMonthIn 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:

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
$lastDayOfPreviousMonthThis 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:

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-LastDayOfPreviousMonthThis 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:

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:
- PowerShell Get Day of Week
- How to Get Yesterday’s Date in PowerShell
- Get the Last Friday of the Month Using PowerShell
- Get the First Day of Next Month in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.