Home » PowerShell » PowerShell Get-Date Minus 1 Day

PowerShell Get-Date Minus 1 Day

In PowerShell to Get-Date minus 1 day date or get yesterday’s date, use the Get-Date cmdlet. The Get-Date has AddDays() function that takes -1 day as an input parameter and returns the previous day’s date.

(Get-Date).AddDays(-1)

PowerShell Get-Date cmdlet returns the current date and time. It provides different methods like AddDays, AddHours, and so on to get the required date and its format as per the requirement.

In this article, we will discuss how to get date minus one day or a previous date using the Get-Date cmdlet in PowerShell.

PowerShell Get Date Minus 1 Day (Previous Day Date)

PowerShell Get-Date cmdlet returns the current date and time, to get date minus 1 day date, use the following PowerShell script.

# Get current date
$today = Get-Date

# Use AddDays method and substract 1 from it to get yesterday date
$yesterday = $today.AddDays(-1)     

In the above PowerShell script, the Get-Date returns the current date time and stores it in the $today variable. It uses AddDays() method to subtract 1 day from $today variable to get the previous date in PowerShell.

The output of the above PowerShell script returns the get-date minus 1 day as given below:

PowerShell Get-Date Minus 1 day
PowerShell Get-Date Minus 1 day

In case, if you don’t want to use the variables and are interested to get the current date minus 1 date, use the below PowerShell script.

(Get-Date).AddDays(-1)

In the above PowerShell script, the Get-Date cmdlet is wrapped in parentheses to use AddDays() method to subtract 1 day from the date.

The output of the above script returns the previous date.

PS C:\Windows> Get-Date                                                                                                 
21 January 2023 17:23:24


PS C:\Windows> (Get-Date).AddDays(-1)                                                                                   
20 January 2023 17:23:37


PS C:\Windows>   

Conclusion

I hope the above article on how to get date minus 1 day in PowerShell using the Get-Date cmdlet is helpful to you.

The Get-Date cmdlet in PowerShell returns the DateTime object and provides different methods to manipulate the date.

To know more about the Get-Date methods and properties available, use the Get-Members cmdlet in PowerShell as given below:

Get-Date | Get-Members

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Recommended Content

How to Convert String to DateTime in PowerShell

PowerShell Epoch Time and DateTime Conversion

PowerShell Compare Dates

Convert Timestamp to date in PowerShell