In PowerShell, you can get the date minus 1 day or the previous day using multiple ways.
The following methods show how you can do it.
Method 1: Using the Get-Date with AddDays(-1) method
# get the current date
$currentDate = Get-Date
# substract one day from the current date
$previousDate = $currentDate.AddDays(-1)
# output the previous day's date
Write-Output "Yesterday's date is: $previousDate"
This example will output the date of the previous day.
Method 2: Using the [DateTime] with AddDays(-1) method
# get the current date
$currentDate = [DateTime]::Now
# substract one day from the current date
$previousDate = $currentDate.AddDays(-1)
# output the previous day's date
Write-Output "Yesterday's date is: $previousDate"
This example will output the current date minus 1 day (the previous day’s date).
The following examples show how to use these methods to get the previous day’s date.
Using Get-Date to Get Previous Day Date
To get the date of the previous day in PowerShell, you can use the Get-Date cmdlet and subtract one day from the current date.
The following example shows how you can do it with syntax.
# get the current date $currentDate = Get-Date # substract one day from the current date $previousDate = $currentDate.AddDays(-1) # output the previous day's date Write-Output "Yesterday's date is: $previousDate"
Output:
In this example, $currentDate contains the current date. We subtract 1 day from the $currentDate using AddDay(-1) and assign it to the $previousDate.
Finally, we display the previous day’s date using the Write-Output cmdlet.
After running the script, it will display the previous day’s date. For example, if today’s date is “17-April-2024“, it will display “16-April-2024 12:00:00 PM”.
The following example shows how to get the previous day’s date in the “dd-MM-yyyy” format.
If you want to get the previous day’s date in a specific format, use the ToString() method or the -Format parameter with the desired format string.
# Get the previous day's date in dd-MM-yyyy format $previousDate = (Get-Date).AddDays(-1).ToString("dd-MM-yyyy") Write-Output "Yesterday's date is: " + $previousDate
This will output the previous day’s date in the specified format.
Yesterday's date is: 16-04-2024
Using the [DateTime]::Now with AddDay(-1) to Get Previous Date
Another way to get the previous day’s date is by using the [DateTime] struct and its Now and AddDays() methods.
The following example shows how you can do it with syntax.
# get the current date $currentDate = [DateTime]::Now # substract one day from the current date $previousDate = $currentDate.AddDays(-1) # output the previous day's date Write-Output "Yesterday's date is: $previousDate"
Output:
In this example, it outputs the date of the previous day.
If you want to get the previous day’s date in a specific format, use the ToString() method with the desired format string.
The following example shows how to get the previous day’s date in the “dd-MM-yyyy” format.
# Get the previous day's date in dd-MM-yyyy format $previousDate = [DateTime]::Now.AddDays(-1).ToString("dd-MM-yyyy") Write-Output "Yesterday's date is: $previousDate"
This example will output the previous day’s date in the specified format.
Yesterday's date is: 16-04-2024
Conclusion
I hope the above article on getting yesterday’s date or the previous day’s date in PowerShell is helpful to you.
You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.