To get yesterday’s date in PowerShell, subtract one day from today with (Get-Date).AddDays(-1). The same AddDays() method adds days to any date, and a negative number goes back in time:
$today = [datetime]'2026-10-23 08:15'
"Yesterday: $($today.AddDays(-1).ToString('yyyy-MM-dd'))"
"In 30 days: $($today.AddDays(30).ToString('MMM d, yyyy'))"
"One week earlier: $($today.AddDays(-7).ToString('MMM d, yyyy'))"
Output:
Yesterday: 2026-10-22
In 30 days: Nov 22, 2026
One week earlier: Oct 16, 2026
I pinned the examples to fixed 2026 dates so your output matches mine. In a real script, start from Get-Date instead, and the results follow the clock.
Get yesterday’s date with (Get-Date).AddDays(-1)
These three lines are the ones you’ll use most. Run them in any console and you’ll see dates one day back from right now:
(Get-Date).AddDays(-1) # yesterday, same time of day as now
(Get-Date).Date.AddDays(-1) # yesterday at 12:00 AM
[datetime]::Today.AddDays(-1) # same as the line above
The first line keeps the current time, so at 3:40 PM it returns yesterday at 3:40 PM. The other two return yesterday at midnight, which is what you want as the start of a search window.
Here’s the same idea on a fixed date, with the day name added:
$now = [datetime]'2026-10-23 14:35:10'
"AddDays(-1): $($now.AddDays(-1))"
"Midnight start: $($now.Date.AddDays(-1))"
"Formatted: $($now.AddDays(-1).ToString('yyyy-MM-dd'))"
"Day of the week: $($now.AddDays(-1).DayOfWeek)"
Output:
AddDays(-1): 10/22/2026 14:35:10
Midnight start: 10/22/2026 00:00:00
Formatted: 2026-10-22
Day of the week: Thursday

A date in a string like "$(...)" always prints month/day/year with a 24-hour clock, whatever your regional settings are. Call ToString() with a format when you need a specific layout.
Is [datetime]::Today the same as (Get-Date).Date?
Yes. Both give today’s date with the time set to 00:00:00 on your local clock, so subtracting a day from either gives yesterday at midnight:
"[datetime]::Today equals (Get-Date).Date: $([datetime]::Today -eq (Get-Date).Date)"
"Time of day on [datetime]::Today: $([datetime]::Today.TimeOfDay)"
"Time of day on yesterday at midnight: $([datetime]::Today.AddDays(-1).TimeOfDay)"
Output:
[datetime]::Today equals (Get-Date).Date: True
Time of day on [datetime]::Today: 00:00:00
Time of day on yesterday at midnight: 00:00:00
That exact midnight matters when you start comparing dates in PowerShell, because 12:00 AM and 12:00:00.250 AM aren’t equal.
What (Get-Date).AddDays(0) does
AddDays(0) returns the same date and time, not today at midnight. Use .Date for that:
$now = [datetime]'2026-10-23 14:35:10'
"AddDays(0): $($now.AddDays(0))"
".Date: $($now.Date)"
Output:
AddDays(0): 10/23/2026 14:35:10
.Date: 10/23/2026 00:00:00
Get today at midnight the reliable way
Setting the hour, minute and second with Get-Date looks like midnight, but the milliseconds stay behind, so equality checks fail:
$today = Get-Date -Hour 0 -Minute 0 -Second 0
"Get-Date -Hour 0 -Minute 0 -Second 0 leaves milliseconds: $($today.Millisecond -ne 0 -or $today.Ticks % 10000 -ne 0)"
"(Get-Date).Date is exactly midnight: $((Get-Date).Date.TimeOfDay -eq [timespan]::Zero)"
Output:
Get-Date -Hour 0 -Minute 0 -Second 0 leaves milliseconds: True
(Get-Date).Date is exactly midnight: True
(Get-Date).Date and [datetime]::Today clear the whole time part, milliseconds included.
How do I format yesterday’s date as text?
Most scripts need yesterday as a string for a file name, a report title or a log search. You have four tools, and one of them behaves differently in Windows PowerShell 5.1:
$yesterday = ([datetime]'2026-10-23 08:15').AddDays(-1)
"ToString: $($yesterday.ToString('yyyy-MM-dd'))"
"-Format: $(Get-Date -Date $yesterday -Format 'yyyy-MM-dd')"
"FileDate: $(Get-Date -Date $yesterday -Format FileDate)"
"-UFormat %F: $(Get-Date -Date $yesterday -UFormat '%F')"
"Long date: $($yesterday.ToString('dddd, MMMM d, yyyy'))"
"File name: sales-$($yesterday.ToString('yyyyMMdd')).csv"
Output:
ToString: 2026-10-22
-Format: 2026-10-22
FileDate: 20261022
-UFormat %F: 2026-10-22
Long date: Thursday, October 22, 2026
File name: sales-20261022.csv

Run the same script in Windows PowerShell 5.1 and the %F line prints just the letter F:
