One of my team members was recently required to get yesterday’s date in PowerShell. There are different methods to achieve this. In this tutorial, I will explain how to get Yesterday’s date in PowerShell with examples.
To get yesterday’s date in PowerShell, you can use the Get-Date cmdlet combined with the AddDays() method. Simply run the command (Get-Date).AddDays(-1) to retrieve a date object representing the previous day.
Get Yesterday’s Date in PowerShell
PowerShell’s Get-Date cmdlet allows you to retrieve the current date and time. However, it can do much more than just that. By using the AddDays() method, you can easily manipulate dates to get yesterday’s date.
Syntax
The basic syntax to get yesterday’s date in PowerShell is:
(Get-Date).AddDays(-1)This command creates a date and time object for today and subtracts one day from it.
The Get-Date cmdlet returns the current date and time. By using the AddDays() method with a parameter of -1, we can subtract one day from the current date.
Now, let me show you different methods to get yesterday’s date.
Check out Add Days to a Date in PowerShell
Method 1: Using Get-Date with AddDays()
The best and most recommended way to get yesterday’s date is by using the Get-Date cmdlet combined with the AddDays() method. Here’s how you can do it:
$yesterday = (Get-Date).AddDays(-1)
Write-Output $yesterdayThis script stores yesterday’s date in the $yesterday variable and then outputs it.
Here is the output you can see in the screenshot below:

Method 2: Using DateTime Class
Another way to get yesterday’s date in PowerShell is by using the [DateTime] class. This method is particularly useful if you prefer working with .NET classes directly:
$yesterday = [DateTime]::Today.AddDays(-1)
Write-Output $yesterdayThis command uses the [DateTime]::Today property to get today’s date at midnight and then subtracts one day from it.
I executed the above PowerShell script using VS code, and you can see the output in the screenshot below:

Read Get Date Differences Between Two Dates in PowerShell
Method 3: Format the Date
Sometimes, you might need yesterday’s date in a specific format in PowerShell. You can achieve this by using the ToString() method:
$yesterday = (Get-Date).AddDays(-1).ToString("yyyy-MM-dd")
Write-Output $yesterdayThis script formats yesterday’s date as yyyy-MM-dd, which is a common date format used in many applications.
Method 4: Get Yesterday’s Date and Time
If you need to get yesterday’s date at a specific time, you can use the following method:
$yesterdayAtTenPM = (Get-Date).AddDays(-1).Date.AddHours(22)
Write-Output $yesterdayAtTenPMThis command first gets yesterday’s date, sets the time to midnight, and then adds 22 hours to get 10:00 PM.
You can see the exact output in the screenshot below:

Conclusion
In this tutorial, I’ve shown you several methods to get yesterday’s date in PowerShell. I have shown examples of using the Get-Date cmdlet or the [DateTime] class, for getting yesterday’s date. Still, do you have any questions? Feel free to leave a comment below.
You may also like the following tutorials:
- Get Date and Time in 24-Hour Format in PowerShell
- PowerShell Get-Date Minus 1 Day
- PowerShell Get-Date Format ISO 8601
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.