All in all cleaning scirpt save as ps1 - moexalter/mowindows GitHub Wiki

Function to calculate folder size

function Get-FolderSize { param ([string]$folderPath) if (Test-Path -Path $folderPath) { $folderSize = (Get-ChildItem -Path $folderPath -Recurse -File | Measure-Object -Property Length -Sum).Sum return [math]::Round($folderSize / 1GB, 2) } return 0 }

Function to display results

function Show-Results { param ([string]$task, [double]$spaceFreed) Write-Host "$task freed: $spaceFreed GB" }

Main cleanup script

Write-Host "=== Windows Cleanup Script ===" Write-Host "This script will help you clear unnecessary files from your system." Write-Host "You will be prompted for each task. Enter 'Y' to proceed or 'N' to skip."

1. Clear Temporary Files

$choice = Read-Host "Do you want to clear temporary files? (Y/N)" if ($choice -eq 'Y' -or $choice -eq 'y') { $initialSize = Get-FolderSize -folderPath "$env:TEMP" Remove-Item -Path "$env:TEMP*" -Recurse -Force -ErrorAction SilentlyContinue Remove-Item -Path "C:\Windows\Temp*" -Recurse -Force -ErrorAction SilentlyContinue $spaceFreed = $initialSize - (Get-FolderSize -folderPath "$env:TEMP") Show-Results -task "Temporary files" -spaceFreed $spaceFreed }

2. Clear Windows Update Cache

$choice = Read-Host "Do you want to clear Windows Update cache? (Y/N)" if ($choice -eq 'Y' -or $choice -eq 'y') { $initialSize = Get-FolderSize -folderPath "C:\Windows\SoftwareDistribution" Stop-Service -Name wuauserv -Force Remove-Item -Path "C:\Windows\SoftwareDistribution*" -Recurse -Force -ErrorAction SilentlyContinue Start-Service -Name wuauserv $spaceFreed = $initialSize - (Get-FolderSize -folderPath "C:\Windows\SoftwareDistribution") Show-Results -task "Windows Update cache" -spaceFreed $spaceFreed }

3. Clear Recycle Bin

$choice = Read-Host "Do you want to empty the Recycle Bin? (Y/N)" if ($choice -eq 'Y' -or $choice -eq 'y') { $initialSize = Get-FolderSize -folderPath "C:`$Recycle.Bin" Clear-RecycleBin -Force -ErrorAction SilentlyContinue $spaceFreed = $initialSize - (Get-FolderSize -folderPath "C:`$Recycle.Bin") Show-Results -task "Recycle Bin" -spaceFreed $spaceFreed }

4. Clear Thumbnail Cache

$choice = Read-Host "Do you want to clear thumbnail cache? (Y/N)" if ($choice -eq 'Y' -or $choice -eq 'y') { $initialSize = Get-FolderSize -folderPath "$env:LOCALAPPDATA\Microsoft\Windows\Explorer" Remove-Item -Path "$env:LOCALAPPDATA\Microsoft\Windows\Explorer\thumbcache_*.db" -Force -ErrorAction SilentlyContinue $spaceFreed = $initialSize - (Get-FolderSize -folderPath "$env:LOCALAPPDATA\Microsoft\Windows\Explorer") Show-Results -task "Thumbnail cache" -spaceFreed $spaceFreed }

5. Clear Prefetch Files

$choice = Read-Host "Do you want to clear Prefetch files? (Y/N)" if ($choice -eq 'Y' -or $choice -eq 'y') { $initialSize = Get-FolderSize -folderPath "C:\Windows\Prefetch" Remove-Item -Path "C:\Windows\Prefetch*" -Recurse -Force -ErrorAction SilentlyContinue $spaceFreed = $initialSize - (Get-FolderSize -folderPath "C:\Windows\Prefetch") Show-Results -task "Prefetch files" -spaceFreed $spaceFreed }

6. Clear DNS Cache

$choice = Read-Host "Do you want to clear DNS cache? (Y/N)" if ($choice -eq 'Y' -or $choice -eq 'y') { Write-Host "Clearing DNS cache..." ipconfig /flushdns Write-Host "DNS cache cleared." }

7. Clear Microsoft Store Cache

$choice = Read-Host "Do you want to clear Microsoft Store cache? (Y/N)" if ($choice -eq 'Y' -or $choice -eq 'y') { $initialSize = Get-FolderSize -folderPath "$env:LOCALAPPDATA\Packages\Microsoft.WindowsStore_\LocalCache" Get-ChildItem -Path "$env:LOCALAPPDATA\Packages\Microsoft.WindowsStore_\LocalCache" | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue $spaceFreed = $initialSize - (Get-FolderSize -folderPath "$env:LOCALAPPDATA\Packages\Microsoft.WindowsStore_*\LocalCache") Show-Results -task "Microsoft Store cache" -spaceFreed $spaceFreed }

8. Clear System Error Memory Dumps

$choice = Read-Host "Do you want to clear system error memory dumps? (Y/N)" if ($choice -eq 'Y' -or $choice -eq 'y') { $initialSize = Get-FolderSize -folderPath "C:\Windows\Minidump" Remove-Item -Path "C:\Windows\Minidump*" -Recurse -Force -ErrorAction SilentlyContinue Remove-Item -Path "C:\Windows\MEMORY.DMP" -Force -ErrorAction SilentlyContinue $spaceFreed = $initialSize - (Get-FolderSize -folderPath "C:\Windows\Minidump") Show-Results -task "System error memory dumps" -spaceFreed $spaceFreed }

9. Clear Old Windows Installations

$choice = Read-Host "Do you want to clear old Windows installations? (Y/N)" if ($choice -eq 'Y' -or $choice -eq 'y') { $initialSize = Get-FolderSize -folderPath "C:\Windows.old" Remove-Item -Path "C:\Windows.old*" -Recurse -Force -ErrorAction SilentlyContinue $spaceFreed = $initialSize - (Get-FolderSize -folderPath "C:\Windows.old") Show-Results -task "Old Windows installations" -spaceFreed $spaceFreed }

10. Clear Edge Browser Cache

$choice = Read-Host "Do you want to clear Microsoft Edge cache? (Y/N)" if ($choice -eq 'Y' -or $choice -eq 'y') { $initialSize = Get-FolderSize -folderPath "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache" Remove-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache*" -Recurse -Force -ErrorAction SilentlyContinue Remove-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Code Cache*" -Recurse -Force -ErrorAction SilentlyContinue Remove-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Media Cache*" -Recurse -Force -ErrorAction SilentlyContinue $spaceFreed = $initialSize - (Get-FolderSize -folderPath "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache") Show-Results -task "Microsoft Edge cache" -spaceFreed $spaceFreed }

Write-Host "=== Cleanup completed ==="