I have some folders which are named with the following date format 'YYYYMMDD' i.e. 20160902 representing Sept 2nd 2016. I want to delete a folder that is 6 months old including all of its content. I'm able to calculating a date i.e. (Get-Date).AddDays(-150) or I can format the current date (Get-Date -Format FileDateUniversal). (after I get a formatted date, I strip off the last character "z" to get just 20160902). My problem is getting some date in the past, and get it formatted as a string such as YYYYMMDD so that I can use the string to search for and then delete old folders. I've also tried working with formatting a date with the following....
Get-Date -Format "yyyymmdd"
What is odd to me with the above cmdlet, is that my latest result or output is 20160702. I don't know where 07 ( in 20160702) is coming from, but I've gotten different numbers in place of 07 when I've run that cmdlet at different times. I've gotten 10, 33, 52, so I don't know what is going on there.
Is there any way I can calculate a date, 5 or 6 months in the past, convert the date into a string with the format of YYYYMMDD and then use that string or format to search for the folder and delete it if it exists?
Thank you for any assistance,
Dean