PowerShell: Dates and Times
There’s often times when you’ll need to work with dates. Perhaps you’re archiving information and need to establish a cutoff date, or perhaps you’re examining dates from AD and need to calculate how many days it’s been since a certain date. PowerShell generally makes it pretty easy to do.
Need to get a date?
$date = Get-Date
That also gets the current time. Try:
$date | gm
To see what all you can do with that date once you’ve got it. Need a short-formatted date?
$date.toshortdatestring()
Need to see what date it was a year ago?
$date.addyears(-1)
It’s really easy to work with dates using their built-in methods!
Oh, and see the TechNet Script Center for more details on date formatting, if you’re interested.




