I’m wanting to create a monthly archive and keep 4 months worth. Each month i’ll create a new folder for this month and remove the oldest. Whilst I have a check in there to just select the oldest month to remove, i also wanted a bit of belt and braces to check that the folder i’m removing is indeed 4 months old. The issue I have is that i’m going to be running it on the first Monday of each month so date wise this may say have run on the 5th 4 months ago but is running on the 2nd this month, so the following check wouldn’t remove the 4 month old folder, as it’s just shy of 4 months old (obviously if it had run on the 2nd 4 months ago, and the 5th this month it would be fine):
So what i’ve been trying to work out is a way of checking if the original folder is “the 4th month ago” if that makes sense ie Sept, i’ve tried various things all to no avail. Any pointers would be greatly welcomed
Hi and welcome! When you share your code please format it
Next with Get-ChildItem -path "C:\temp" That will get files and folders stored in that directory. You may want to add on the -Directory parameter to filter out non-directories.
If I am understanding you correctly, you’re just concerned about the month yeah? That is, as long the integar month value is 4 months in the future for the current date (or more) you want to proceed. I see you used the month property from the CreationTime but you didn’t do that with the second part. Have you tried something like this:
Hi thanks a lot for your response dotnVo. Yes, I definitely need to specify the -Directory parameter, thanks for that. I tried adding the .Month to the Get-Date but it still had the same result, if the folder was created on a day of the month after the current day of the month it would still leave it there. I had another suggestion posted elsewhere which has done the trick which is to create a new datetime object but adjusted to be the first of the month in which we’re runnnig the script, as below (apologies if it doesn’t format - I wasn’t sure how to do that on this forum)
I thought about it some more and my approach really wasn’t a good one, mostly because if we try to use the month integer that represents it, we do math out of base 10, but that’s not how time works. There’s probably ways to get my approacch to work but it takes more code and work. For example, Jan of 2023 the value of the month is 1, but December of 2023 its 12, however, Jan of 2024 is ‘in the future’, so we really can’t take the month integer alone to do comparisons. Also, consider the fact that a ‘month’ is also a variable piece of data. How long is a month? Well, it depends.
I think there’s perhaps a better approach, but it ultimately depends on your needs.
Separate your tasks into two different tasks. For your "creation’ task, always create it on a specific date (not every first Monday) but… perhaps every first day of the month. Then, for your removal task, run it maybe every month on the 7th. That way, when you do your time comparisons with the AddMonths method, the CreationTime of the folder your checking will be always created on the first of the month, and you are checking against the time where your script for the removal runs, which is the 7th, so you remove the days as part of the equation, as day 1 is always less than day 7. Hopefully that makes sense.
There is a New-TimeSpan cmdlet that might be useful here. You can feed it a start date and end date and while it doesn’t spit out months, it does spit out days. Maybe you can work out what an ‘acceptable’ number of days is. That said, you’ll still run into situations where it won’t catch so you might need to drop the Select-Object -Last 1
On that topic, consider dropping the Select-Object -Last 1. Add any checks that you need but perhaps when a situation happens and it hasn’t been quite 4 months ‘technically’, you just let it go, and clean it up the following month. This seems like the simplest solution to be honest. If it doesn’t matter if the archived folder sticks around another month, it should be cleaned up the following month. That said, I’d still probably implement #1. You just need to have other checks in place, like, make sure you name the folders in a certain pattern and you can filter on those with Get-ChildItem.
Consider naming your folders in such a way so you can not only add those in to your checks, but implement a system where you can compare based on the folder names, based on what you want to do with them.
There are some different ways to get the job done it just depends on what you need really.