PowerShell Get-Date Minus 1 Day

When working with PowerShell scripts, you might encounter situations where you need to manipulate dates, such as finding yesterday’s date or subtracting a day from a specific date. PowerShell’s Get-Date cmdlet is a versatile tool that can help you easily handle such date operations. In this blog post, we’ll explore how to use Get-Date to subtract one day from the current date or a specified date. This is a complete tutorial on “PowerShell Get-Date Minus 1 Day“.

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. By specifying a negative number, users effectively subtract days from the date in question.

Understanding 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.

See also  PowerShell create file if not exists

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."

Subtracting 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


Subtracting a day from a date in PowerShell is straightforward with the Get-Date cmdlet and the AddDays() method. Whether you’re dealing with today’s date or a specific date, you can perform date arithmetic easily and efficiently.

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

You may also like: