How to Get Yesterday’s Date in PowerShell

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 $yesterday

This script stores yesterday’s date in the $yesterday variable and then outputs it.

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

powershell get yesterday date

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 $yesterday

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

get yesterday date in powershell

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 $yesterday

This 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 $yesterdayAtTenPM

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

How to Get Yesterday's Date in PowerShell

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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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