SOLVED

How to compare a date to x months ago when only a part month has passed

Copper Contributor

Hi,

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):

Get-ChildItem -path "C:\temp" |Sort-Object -property creationtime -descending | Select -last 1 | Where CreationTime.month -le (Get-Date).AddMonths(-4) | remove-item

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 :) 

2 Replies
best response confirmed by flakey321 (Copper Contributor)
Solution

@flakey321 

 

Hi, Robert.

 

Given you're counting previous months and not interested in the day of month, then for the current date, we don't care about the day of month, making the easiest and most consistent approach to simply work from the 1st.

 

If I assume that in "keeping the last four months" that the current month is one of those four months, meaning anything older than 1st October 2023 should be removed (i.e. we don't care about what date it is in September that the folder was created), then we can run with something like the following example.

 

Example

Get-ChildItem -Directory -Path "C:\Temp" |
    Where-Object {
        $_.CreationTime -lt ([datetime]::new([datetime]::Now.Year, [datetime]::Now.Month, 1)).AddMonths(-3)
    } |
        Remove-Item -Recurse -Force;

 

What we've done here is created a new datetime object based off today's date but adjusted to be the 1st of the month (i.e. 2024-01-01 as if the time of writing).

 

We then subtract three months from this reference date (since the current month is the fourth month to keep, and you can't practically subtract 0).

 

Anything older than this new reference date is eligible for removal, so this is what we compare the directory's CreationTime against.

 

The Sort-Object and Select-Object from your original example aren't necessary.

 

Cheers,

Lain

@LainRobertson Hi Lain - I like you're approach, v smart :) Yes that works absolutely perfectly and you're assumptions about what I was after are all perfectly correct, I was after keeping this current month when i run the PS to be the latest of the 4 months I want to retain. I've tested with directorys with a specified creation date in Sept one for the 10th and one for the 20th (today being the 15th) and it correctly identifies both of them.

 

Thank you so much for taking the time out to reply, that's been a massive help to me

 

1 best response

Accepted Solutions
best response confirmed by flakey321 (Copper Contributor)
Solution

@flakey321 

 

Hi, Robert.

 

Given you're counting previous months and not interested in the day of month, then for the current date, we don't care about the day of month, making the easiest and most consistent approach to simply work from the 1st.

 

If I assume that in "keeping the last four months" that the current month is one of those four months, meaning anything older than 1st October 2023 should be removed (i.e. we don't care about what date it is in September that the folder was created), then we can run with something like the following example.

 

Example

Get-ChildItem -Directory -Path "C:\Temp" |
    Where-Object {
        $_.CreationTime -lt ([datetime]::new([datetime]::Now.Year, [datetime]::Now.Month, 1)).AddMonths(-3)
    } |
        Remove-Item -Recurse -Force;

 

What we've done here is created a new datetime object based off today's date but adjusted to be the 1st of the month (i.e. 2024-01-01 as if the time of writing).

 

We then subtract three months from this reference date (since the current month is the fourth month to keep, and you can't practically subtract 0).

 

Anything older than this new reference date is eligible for removal, so this is what we compare the directory's CreationTime against.

 

The Sort-Object and Select-Object from your original example aren't necessary.

 

Cheers,

Lain

View solution in original post