[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] param( [string]$Path, [ValidateRange(1, 3650)] [int]$Days = 180, [switch]$Apply ) $ErrorActionPreference = 'Stop' if (-not $Path) { $Path = Join-Path $HOME 'Downloads' } if (-not (Test-Path -LiteralPath $Path -PathType Container)) { throw "Каталог не найден: $Path" } $resolved = (Resolve-Path -LiteralPath $Path).ProviderPath.TrimEnd('\') if ($resolved -eq ([System.IO.Path]::GetPathRoot($resolved)).TrimEnd('\')) { throw 'Нельзя использовать корень диска.' } $extensions = @('.msi', '.exe') $cutoff = (Get-Date).AddDays(-$Days) $files = @( Get-ChildItem -LiteralPath $resolved -File -Recurse -Force -ErrorAction SilentlyContinue | Where-Object { $extensions -contains $_.Extension.ToLowerInvariant() -and $_.LastWriteTime -lt $cutoff } ) $bytes = ($files | Measure-Object -Property Length -Sum).Sum if ($null -eq $bytes) { $bytes = 0 } Write-Host ("Старых установщиков: {0}, объём: {1:N1} MB" -f $files.Count, ($bytes / 1MB)) $files | Select-Object Name, Length, LastWriteTime, FullName | Format-Table -AutoSize if (-not $Apply) { Write-Host 'Предпросмотр. Для отправки в корзину добавьте -Apply.' return } Add-Type -AssemblyName Microsoft.VisualBasic $removed = 0 $failed = 0 foreach ($file in $files) { if ($PSCmdlet.ShouldProcess($file.FullName, 'Отправить установщик в корзину')) { try { [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile( $file.FullName, [Microsoft.VisualBasic.FileIO.UIOption]::OnlyErrorDialogs, [Microsoft.VisualBasic.FileIO.RecycleOption]::SendToRecycleBin ) $removed++ Write-Host "В корзине: $($file.Name)" } catch { $failed++ Write-Warning "Не удалось отправить $($file.FullName): $($_.Exception.Message)" } } } Write-Host "Отправлено в корзину: $removed, ошибок: $failed"