I'm trying to create a powershellscript to schedule backup delete so that the HDD doesn't get full.
- What I want to do is to verify which file that is the newest
- Afterwards I want to check if the filesize doesn't different more than 10% from the second newest file.
- If the filesize is within the size range then delete all but the newest ones.
- If the filesize is smaller or bigger than 10% of the second newest file then delete all but the newest and the second newest file.
I would like you guys to help me out how I should think to formulate the code to make this to work.
I've started with below which deletes all files older than 2 days but I'm not quite sure how to change that to keep the newest file not depending of days.
$Path = "C:\Temp\Backup\Folder1\"
$Days = 2
$Date = Get-Date
$Include = "*.gpg"
$Exclude = "*.txt"
Get-ChildItem $Path -Recurse |
Where-Object {-not $_.PSIsContainer -and $Date.Subtract($_.CreationTime).Days -gt $Days } |
Remove-Item -WhatIf