How to Get Yesterday’s Date in PowerShell (With Examples)


You can use the following methods to get yesterday’s date in PowerShell:

Method 1: Get Yesterday’s Date

[DateTime]::Today.AddDays(-1)

This particular example returns yesterday’s date with a default time of 12 AM.

Method 2: Get Yesterday’s Date at Specific Time

[DateTime]::Today.AddDays(-1).AddHours(16)

This particular example returns yesterday’s date with a time of 4 PM.

By using AddDays(-1) we are able to subtract one day from today.

Then, by using AddHours(16) we are able to add 16 hours to get a time of 4 PM.

The following examples show how to use each method in practice.

Example 1: Get Yesterday’s Date in PowerShell

This article is being written on 6/18/2024.

We can use the following syntax in PowerShell to get yesterday’s date:

[DateTime]::Today.AddDays(-1)

The following screenshot shows how to use this syntax in practice:

PowerShell get yesterday's date

Notice that this returns a date of 6/17/2024 at 12AM, which represents the date for yesterday.

Example 2: Get Yesterday’s Date at Specific Time in PowerShell

We can use the following syntax in PowerShell to get yesterday’s date at exactly 4 PM:

[DateTime]::Today.AddDays(-1).AddHours(16)

The following screenshot shows how to use this syntax in practice:

PowerShell get yesterday's date at specific time

Notice that this returns a date of 6/17/2024 at 4 PM.

Feel free to change the value in the AddHours() method to instead return yesterday’s date at a different specific time.

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:

PowerShell: How to Compare Dates
PowerShell: How to Sort an Array of Strings by Date
PowerShell: How to Use Get-Date with 24-Hour Format

Leave a Reply