Retention policy is "keep the last 7 backups (past week), and then keep Friday backups for 3 months. And then keep every month's last Friday backup for one year".
Below is the script which covers two retention conditions, but unable to complete the third condition, which is "keep every month's last Friday backup for one year". Need help in making IF block for same.
EDIT: Solution to move files around in different folders and then apply separate retention scripts on separate folders will not possible as there are lot of sub-folders inside. As this is all for backup files, hence moving files will make our dependent restore process more complicated also as otherwise that code will then require tweaking on all servers too.
#----- define folder where files are located ----#
$TargetFolder = "C:\Users\chsa\Desktop\Retention"
#----- define extension ----#
$Extension = "*.txt"
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)
#----- get files based on lastwrite filter and specified folder ---#
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse
foreach ($File in $Files)
{
if ($File -ne $NULL -and $File.LastWriteTime -lt (Get-Date).AddYears(-1))
{
Remove-Item $File.FullName | Out-Null
Write-Host $File
}
if ($File -ne $NULL -and $File.LastWriteTime -ge (Get-Date).AddMonths(-3) -and $File.LastWriteTime -lt (Get-Date).AddDays(-7) -and $File.LastWriteTime.DayOfWeek -ne "Friday")
{
Remove-Item $File.FullName | Out-Null
Write-Host $File
}
}