[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] param( [switch]$Apply ) $ErrorActionPreference = 'Stop' $directory = Join-Path $env:LOCALAPPDATA 'Microsoft\Windows\Explorer' if (-not (Test-Path -LiteralPath $directory -PathType Container)) { throw "Каталог не найден: $directory" } $files = @( Get-ChildItem -LiteralPath $directory -File -Force -ErrorAction SilentlyContinue | Where-Object { $_.Name -like 'thumbcache_*.db' -or $_.Name -like 'iconcache_*.db' } ) $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 | Format-Table -AutoSize if (-not $Apply) { Write-Host 'Предпросмотр. Для очистки добавьте -Apply.' return } if ($files.Count -eq 0) { Write-Host 'Файлы кэша не найдены.' return } $explorerWasRunning = @(Get-Process -Name explorer -ErrorAction SilentlyContinue).Count -gt 0 if ($explorerWasRunning) { Stop-Process -Name explorer -Force -ErrorAction Stop Start-Sleep -Seconds 2 } try { foreach ($file in $files) { if ($PSCmdlet.ShouldProcess($file.FullName, 'Удалить файл кэша миниатюр')) { try { Remove-Item -LiteralPath $file.FullName -Force -ErrorAction Stop Write-Host "Удалён: $($file.Name)" } catch { Write-Warning "Не удалось удалить $($file.Name): $($_.Exception.Message)" } } } } finally { if ($explorerWasRunning -and -not (Get-Process -Name explorer -ErrorAction SilentlyContinue)) { Start-Process -FilePath explorer.exe Write-Host 'Проводник запущен заново.' } }