[CmdletBinding()] param( [switch]$Empty, [switch]$Force, [string]$DriveLetter ) $ErrorActionPreference = 'Stop' $shell = New-Object -ComObject Shell.Application $bin = $shell.Namespace(10) if ($null -eq $bin) { throw 'Не удалось открыть корзину через оболочку Windows.' } $items = @($bin.Items()) $totalBytes = 0 foreach ($item in $items) { try { $totalBytes += [int64]$item.Size } catch { Write-Verbose "Не удалось прочитать размер элемента $($item.Name)" } } Write-Host ("Элементов в корзине: {0}" -f $items.Count) Write-Host ("Суммарный размер: {0} МБ" -f [math]::Round($totalBytes / 1MB, 2)) if ($items.Count -gt 0) { $items | Select-Object -First 20 Name, @{Name = 'SizeMB'; Expression = { [math]::Round([int64]$_.Size / 1MB, 2) }}, Path | Format-Table -AutoSize } if (-not $Empty) { Write-Host 'Это только отчёт. Для очистки добавьте -Empty.' return } if ($items.Count -eq 0) { Write-Host 'Корзина уже пуста.' return } if (-not $Force) { $answer = Read-Host 'Введите yes, чтобы безвозвратно очистить корзину' if ($answer -ne 'yes') { Write-Host 'Отменено.' return } } if (-not (Get-Command Clear-RecycleBin -ErrorAction SilentlyContinue)) { throw 'Команда Clear-RecycleBin недоступна в этой версии Windows.' } if ($DriveLetter) { Clear-RecycleBin -DriveLetter $DriveLetter -Force -ErrorAction Stop Write-Host "Корзина очищена для диска $DriveLetter." } else { Clear-RecycleBin -Force -ErrorAction Stop Write-Host 'Корзина очищена для всех дисков.' }