For those unfamiliar with DateTime objects, the problem is exceedingly simple – but the solution is even simpler.
Take the following DateTime object:
PS C:UsersAbrodie> $dt = get-date
PS C:UsersAbrodie> $dt
03 November 2011 17:48:56
If I look at the methods available, you can see there’s a conveniently long set of Add methods, enough to satisfy your every whim:
PS C:UsersAbrodie> $dt | Get-Member -MemberType Method | Format-Wide
All those lovely Add* methods, but only one subtract, which takes a TimeSpan object.
I found the following very useful blog entry on Powershell.nu, which details how to construct a TimeSpan instance and pass it to the Subtract method.
However, easiest way to perform a subtraction is simply to use arithmetic logic. I.e.
PS C:UsersAbrodie> $dt.AddDays(-1)
02 November 2011 17:48:56
If you heard a slight cracking sound, as if a hand slapped a forehead quite hard, around 7:23am on Thursday 3rd November, that was me!