I often need to name a file with a date as part of the file name like:
A simple $date = $(get-date).ToString(“MM-dd-yyyy”) works great for today, but I recently had a need to create a file once a week, with a file name of the previous Saturday.
I had come across Arul Kumaravel’s WebLog post “How Can I Determine the Beginning and Ending Date of the Previous Month?” (http://blogs.msdn.com/arulk/archive/2006/11/22/how-can-i-determine-the-beginning-and-ending-date-of-the-previous-month.aspx) when I was trying to find the first and last days of a month for a SQL query, and so I grabbed that bit of code and created this:
That worked great, but it’s good only for the previous Saturday. I could envision needing to get the previous Monday, or Friday, or even a day several weeks prior.
This function will do that:
. GetPreviousDate Friday 1
$PreviousDate Would show 12-11-2009 if I ran it the week of December 18th, 2009
. GetPreviousDate Friday
$PreviousDate Would show 12-18-2009 if I ran it the week of December 18th, 2009
. GetPreviousDate Wednesday 35
$PreviousDate Would show 04-15-2009 if I ran it the week of December 18th, 2009