PowerShell Get-Date Minus 1 Day

In this tutorial, I will explain how to subtract one day from a date using PowerShell. Sometimes, you need to calculate the previous day’s date in your PowerShell scripts, such as generating reports or automating tasks. I recently faced this issue while creating a script to analyze daily sales data for a client in the USA. Here, you will learn how to use Get-Date to subtract one day from the current date or a specified date.

To subtract one day from the current date in PowerShell, users utilize the Get-Date cmdlet in combination with the AddDays() method. This method is part of the DateTime object acquired from Get-Date, which represents date and time. AddDays() takes one parameter: the number of days to add to the instance of the date. Users effectively subtract days from the date in question by specifying a negative number.

PowerShell Get-Date

First, let’s understand the Get-Date cmdlet. Get-Date is a built-in PowerShell cmdlet that retrieves the current date and time. It can also be used to create and manipulate DateTime objects, which represent dates and times.

PowerShell Get-Date Minus 1 Day

To subtract one day from today’s date, you can use the AddDays() method in PowerShell with Get-Date. This method is available on any DateTime object, which you can get by using Get-Date. Here’s how you can get yesterday’s date:

$yesterday = (Get-Date).AddDays(-1)
Write-Host "Yesterday's date was: $yesterday"

In this example, (Get-Date) retrieves the current date and time. The .AddDays(-1) method subtracts one day, and the result is stored in the $yesterday variable. The Write-Host cmdlet then outputs the result to the console.

You can see the output in the screenshot below after I executed the script using VS Code.

PowerShell Get-Date Minus 1 Day

If you want to write the script for “PowerShell get-date minus 1 day” with a try-catch block, then you can write the script below.

try {
  $yesterday = (Get-Date).AddDays(-1)
} catch {
  Write-Error "An error occurred calculating yesterday's date."
}

Similarly, when subtracting days across a daylight saving change, one should verify the time has been adjusted correctly. An if statement can validate this adjustment:

$yesterday = (Get-Date).AddDays(-1)
if ($yesterday.IsDaylightSavingTime() -ne (Get-Date).IsDaylightSavingTime()) {
  Write-Output "Daylight saving time adjustment may be required."
}

Check out How to Get-Date Without Time in PowerShell?

Subtract One Day from a Specific Date in PowerShell

You may also want to subtract a day from a date that is not today. To do this, you first need to create a DateTime object for the specific date. You can then call the AddDays() method with -1 as the parameter to subtract a day. Here’s an example and the PowerShell script:

# Create a DateTime object for a specific date
$specificDate = Get-Date '2024-01-04'

# Subtract one day from the specified date
$dayBeforeSpecificDate = $specificDate.AddDays(-1)

Write-Host "One day before $specificDate was: $dayBeforeSpecificDate"

In this code, Get-Date '2024-01-04' creates a DateTime object for January 4, 2024. The AddDays(-1) method subtracts one day, and the result is output to the console.

Here is the screenshot for the output:

powershell get date minus 1 day

Conclusion

I have explained various ways to subtract a day from a date in this tutorial using PowerShell. We learned how to use the AddDays() method to subtract a day from the current date or a specific date stored in a variable.

In this PowerShell tutorial, I have explained how to work with PowerShell get-date minus 1 day with examples.

You may also like:

>
Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

Power Platform Tutorial

FREE Power Platform Tutorial PDF

Download 120 Page FREE PDF on Microsoft Power Platform Tutorial. Learn Now…