Upgrade existing script with save 1st and 15th file

i have manage to make some script which delete all files and leave only 1st and 15th day.
So idea is now, that i want to leave also past month, and trigger this script in a 2 months (i will do it with task scheduller)

$Path = "D:\test"

$Daysback = -15

$CurrentDate = Get-Date

$DatetoDelete = $CurrentDate.AddDays($Daysback)

 

Get-ChildItem $Path -Recurse |

    ? { ($_.LastWriteTime -lt $DatetoDelete) -and !($_.LastWriteTime.Day -eq 1) -and !($_.LastWriteTime.Day -eq 15)}|Remove-Item  -Recurse -Confirm:$false -Force

I keep pushing my self, cant figure out how to make this, i cant just increase days, because some of the months have 30 and 31 day…

You will have to build the dates for the 1st and 15st of the last month from its pieces … like this:

$FirstLastMonth = Get-Date -Day 1 -Month $CurrentDate.AddMonths(-1).Month -Year $CurrentDate.AddMonths(-1).Year
$FifteenthLastMonth = Get-Date -Day 15 -Month $CurrentDate.AddMonths(-1).Month -Year $CurrentDate.AddMonths(-1).Year

I tried to add this 2 variables like this

Get-ChildItem $Path -Recurse |

    ? { ($_.LastWriteTime -lt $DatetoDelete) -and !($_.LastWriteTime.Day -eq 1) -and !($_.LastWriteTime.Day -eq 15) -and !($FirstLastMonth) -and !($FifteenthLastMonth)}|Remove-Item  -Recurse -Confirm:$false -Force

but not working. Am i wrong somewhere?

Actually thinking it really through you already have everything you need to catch ALL firsts and fifteenths from the past.
Please do yourself and others a favor and start formatting your code nicely and avoid using aliasses. That makes your code easier to read and easier to understand.

$Path = 'D:\test'
$Daysback = -15
$CurrentDate = Get-Date 
$DatetoDelete = $CurrentDate.AddDays($Daysback)

Get-ChildItem $Path -Recurse |
Where-Object { 
    ($_.LastWriteTime -lt $DatetoDelete) -and
    (
        -not $_.LastWriteTime.Day -eq 1 -or 
        -not $_.LastWriteTime.Day -eq 15
    )
} |
Remove-Item  -Recurse -Confirm:$false -Force

That will catch ALL files older than 15 days and NOT the first of the month or the fifteenth … from every month in the past - not just the last one!! :wink:

Thanks for advice.
I am “girl for everyting” and i do my best for everything!

Can i add task every 6th month to delete all files older than 6th month but leave last two months

Get-ChildItem $Path -Recurse -Force -ea 0 |
? {!$_.PsIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-210)} |
ForEach-Object {
   $_ | del -Force
   $_.FullName | Out-File C:\log\deletedlog.txt -Append
}

but is this going to work?
I want every 6th month to delete all and ofcourse leave past two months not removed.

So in the June 1st when i delete all, 1st and 15th files of April and May must be here.
Dont sure if this what i added will work.
Should I add this as a seperate script

If you’re not sure - test it!! Take some example files - ideally copy the original files to a test folder and test your script logic. You learn the most when you figure it out by yourself. :wink:

Since PowerShell version 3.0 you do not need this anymore: !$_.PsIsContainer . :point_up_2: Get-ChildItem has a parameter -File or -Directory - use them if needed. You should always read the complete help for cmdlets you’re about to use - including the examples to learn how they work.

If you want to add more than 30 days you should use another unit instead of days because it’s harder to read if you have to calculate in your mind first to understand what time span it is about. You can use .AddMonths() and you can even combine 2 different units like .AddMonths(6).AddDays(15) for example.

At the end of the day it is your decission but it does not make sense for me at all. If you want to keep the last 2 month you can run the code anytime leaving the last 2 month. :thinking:

for some reason this do nothing. When i run it i dont get delete files which are not 1st and 15th. Nothing happend.

We cannot help you debugging your scripts. We cannot see your environment and we cannot see what you actually do.