PowerShell: Getting Relative Dates

Here’s a quick and fast way of generating relative dates in any format:

(Get-Date).AddDays(-1).ToString('yyyy-MM-dd')

This would return yesterday’s date in the format year-month-day. Remove the ToString() method if you want the relative date as a true DateTime object.

This returns all error events from the System event log in the past 48 hours:

Get-EventLog -LogName System -EntryType Error -After (Get-Date).AddDays(-2)

Leave a Reply

Your email address will not be published.