Готово сейчас (:
# Initiate the intervals in days by which the folders will be kept
$savedays = @('7', '14', '21', '30', '60', '90', '120', '150', '180', '210', '240', '270', '300', '330', '360')
# initiate array of folders to be deleted
$killlist = @()
# Start a for loop until hitting the one before last element of $savedays
For ($i=0;$i -lt ($savedays.Length -1 ); $i++) {
# Get list of folders in the $path_main
$killlist += @(Get-ChildItem $path_main |
# Newest first
Sort-Object -Property LastWriteTime -Descending |
# Between one and next elevement of #savedays, i.e. between 7 days ago and 14 days ago
Where-Object { $_.LastWriteTime -lt (get-date).AddDays(-$savedays[$i]) -and $_.LastWriteTime -gt (get-date).AddDays(-$savedays[$i+1])} |
# Exclude the most recent folder from the aforementioned period
Select-Object -Skip 1
)
}
# delete folders
foreach ($folder in $killlist) {
# delete the folder including contents
Remove-Item $folder.fullname –recurse
}