[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [string]$Pattern = '*.zip', [ValidateRange(1, 10000)] [int]$Keep = 10, [switch]$Apply ) $ErrorActionPreference = 'Stop' if (-not (Test-Path -LiteralPath $Path -PathType Container)) { throw "Папка не найдена: $Path" } $full = (Resolve-Path -LiteralPath $Path).Path $files = @(Get-ChildItem -LiteralPath $full -File -Filter $Pattern | Sort-Object LastWriteTime -Descending) if ($files.Count -le $Keep) { "Файлов: $($files.Count), лимит: $Keep. Ротация не нужна." exit 0 } $old = @($files | Select-Object -Skip $Keep) $total = [long]0 foreach ($file in $old) { $total += $file.Length "{0} {1} {2:N1} МБ" -f $file.LastWriteTime.ToString('yyyy-MM-dd HH:mm'), $file.Name, ($file.Length / 1MB) } "К удалению: $($old.Count), освободится: $([math]::Round($total / 1MB, 1)) МБ" if (-not $Apply) { "Сухой прогон: добавьте -Apply для удаления." exit 0 } foreach ($file in $old) { Remove-Item -LiteralPath $file.FullName -Force } "Удалено файлов: $($old.Count)"