PowerShell Get-Date Minus 1 Day

Recently, one of my team members needed to get the date for the previous day using PowerShell. I provided the solution for this. In this tutorial, I will explain everything about “PowerShell Get-Date minus 1 day.”

To get the previous day’s date in PowerShell, you can use the Get-Date cmdlet combined with the AddDays method. Simply run (Get-Date).AddDays(-1), which retrieves the current date and subtracts one day from it.

PowerShell Get-Date Minus 1 Day

The Get-Date cmdlet in PowerShell is used to retrieve the current date and time. It’s incredibly useful for scripting and automation tasks where date and time manipulation is required. The syntax is like below:

Get-Date

This command will return the current date and time. However, what if we need the date for the previous day? Let’s explore how to do that.

Subtract Days from the Current Date

To get the date for the previous day, you can subtract one day from the current date. PowerShell makes this easy with the AddDays method. Here’s the syntax:

(Get-Date).AddDays(-1)

In this command:

  • Get-Date retrieves the current date and time.
  • AddDays(-1) subtracts one day from the current date.

Example

Let’s see this in action:

$yesterday = (Get-Date).AddDays(-1)
Write-Output $yesterday

Running this script will output the date for the previous day. Here is the exact output you can see in the screenshot below:

powershell get-date minus 1 day

Read How to Get Date Without Time in PowerShell?

Alternative Methods to Get the Previous Day’s Date

While the AddDays method is the most straightforward; there are other ways to achieve the same result. Let me show you a few other alternatives.

Using New-TimeSpan

The New-TimeSpan cmdlet can be used to create a time interval, which can then be subtracted from the current date. Here’s how you can do it:

$timeSpan = New-TimeSpan -Days 1
$yesterday = (Get-Date) - $timeSpan
Write-Output $yesterday

In this example:

  • New-TimeSpan -Days 1 creates a time span of one day.
  • Subtracting this time span from the current date gives us the previous day’s date.

Here is the exact output in the screenshot below:

powershell get date minus 1 day

Using Date Arithmetic

You can also perform date arithmetic directly using PowerShell’s DateTime object. Here’s an example:

$currentDate = Get-Date
$yesterday = $currentDate.AddDays(-1)
Write-Output $yesterday

This method is similar to the first example but explicitly stores the current date in a variable before subtracting one day.

You can see the exact output in the screenshot below:

powershell subtract days from date

Check out How to Format Date in PowerShell

PowerShell Get-Date Minus 1 Day Examples

Now, let me show you a few real examples of how to get the previous day’s date, which can be incredibly useful in various scenarios. Here are a few practical examples:

Log File Management

If you manage log files and need to archive or process logs from the previous day, this technique can automate the task.

$logDate = (Get-Date).AddDays(-1).ToString("yyyyMMdd")
$logFileName = "log_$logDate.txt"
# Further processing with $logFileName

Scheduled Tasks

When setting up scheduled tasks, you might need to reference the previous day’s date for data processing or reporting.

$reportDate = (Get-Date).AddDays(-1).ToString("MM-dd-yyyy")
# Generate report for $reportDate

Conclusion

In this tutorial, I’ve shown you how to use PowerShell’s Get-Date cmdlet to get the previous day’s date. I have shown different methods like the AddDays method, New-TimeSpan, or direct date arithmetic, etc. I hope you know how to get date minus 1 day in PowerShell.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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