Getting Relative Dates

by Aug 3, 2012

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 hour:

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

Twitter This Tip! ReTweet this Tip!