You can use the following syntax to get the last day of the current month in PowerShell:
(Get-Date -Day 1).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd")
This particular example will return the last day of the current month formatted as yyyy-MM-dd.
Here is how this syntax works:
- First, we use Get-Date -Day 1 to first return the first day of the current month.
- We then use AddMonths(1) to add one month and get the date for the first day of the next month.
- We then use AddDays(-1) to subtract one day to get the last day of the current month.
- Lastly, we use ToString(“yyyy-MM-dd”) to format the resulting date in a specific way.
The following example shows how to use this syntax in practice.
Example: How to Get Last Day of Month in PowerShell
This article is currently being written on 7/18/2024.
We can use the following syntax to get the last day of the current month in PowerShell:
(Get-Date -Day 1).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd")
The following screenshot shows how to use this syntax in practice:

This returns 2024-07-31, which represents the last day of the current month in a yyyy-MM-dd format.
Note that you can specify any date format that you’d like within the ToString() function in PowerShell.
For example, we can use the following syntax to return the last day of the current month in a MM/dd/yyyy format instead:
(Get-Date -Day 1).AddMonths(1).AddDays(-1).ToString("MM/dd/yyyy")
The following screenshot shows how to use this syntax in practice:

This returns 07/31/2024, which represents the last day of the current month in a MM/dd/yyyy format.
Feel free to specify any date format you’d like within the ToString() function.
Note: You can find the complete documentation for the Get-Date cmdlet in PowerShell here.
Related Tutorials
The following tutorials explain how to perform other common tasks in PowerShell:
How to Compare Dates in PowerShell
How to Get Month Name in PowerShell
How to Get Date Without Time in PowerShell
How to Format a DateTime in PowerShell
