Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Just Delete It - A PowerShell tool to free up disk space by safely removing temporary and system files.
- .DESCRIPTION
- This script cleans various system and temporary folders to reclaim disk space without affecting user files.
- It shows detailed progress for each operation and works with Windows 10/11.
- Run as administrator for best results.
- .NOTES
- Version: 2.0
- #>
- # Suppress non-critical errors and warnings
- $ErrorActionPreference = "SilentlyContinue"
- $WarningPreference = "SilentlyContinue"
- # Function for colored console output
- function Write-ColorOutput {
- param (
- [string]$Message,
- [string]$ForegroundColor = "White"
- )
- $originalColor = $host.UI.RawUI.ForegroundColor
- $host.UI.RawUI.ForegroundColor = $ForegroundColor
- Write-Output $Message
- $host.UI.RawUI.ForegroundColor = $originalColor
- }
- # Function to get folder size
- function Get-FolderSize {
- param (
- [string]$Path
- )
- if (Test-Path -Path $Path) {
- $size = (Get-ChildItem -Path $Path -Recurse -Force | Measure-Object -Property Length -Sum).Sum
- return $size
- }
- return 0
- }
- # Function to format file size in human-readable format
- function Format-FileSize {
- param (
- [long]$Size
- )
- if ($Size -ge 1TB) {
- return "{0:N2} TB" -f ($Size / 1TB)
- }
- elseif ($Size -ge 1GB) {
- return "{0:N2} GB" -f ($Size / 1GB)
- }
- elseif ($Size -ge 1MB) {
- return "{0:N2} MB" -f ($Size / 1MB)
- }
- elseif ($Size -ge 1KB) {
- return "{0:N2} KB" -f ($Size / 1KB)
- }
- else {
- return "$Size Bytes"
- }
- }
- # Function to clean Windows temporary folders
- function Clear-WindowsTemp {
- Write-ColorOutput "Cleaning Windows Temp Folders..." -ForegroundColor Cyan
- $totalReclaimed = 0
- $locations = @{
- "Windows Temp" = "$env:windir\Temp";
- "User Temp" = "$env:TEMP";
- "System Temp" = "$env:SystemRoot\Temp";
- "Prefetch" = "$env:windir\Prefetch";
- "Windows Error Reports" = "$env:LOCALAPPDATA\Microsoft\Windows\WER";
- "Internet Explorer Cache" = "$env:LOCALAPPDATA\Microsoft\Windows\INetCache";
- "Setup Log Files" = "$env:windir\Logs"
- }
- foreach ($location in $locations.GetEnumerator()) {
- $name = $location.Key
- $path = $location.Value
- Write-ColorOutput " Processing $name..." -ForegroundColor Gray
- if (Test-Path -Path $path) {
- $initialSize = Get-FolderSize -Path $path
- Write-ColorOutput " Size before cleanup: $(Format-FileSize -Size $initialSize)" -ForegroundColor Gray
- # Clean files
- Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer } | ForEach-Object {
- try {
- Remove-Item -Path $_.FullName -Force
- Write-Progress -Activity "Cleaning $name" -Status "Removing $($_.Name)" -PercentComplete -1
- } catch {}
- }
- # Clean empty folders
- Get-ChildItem -Path $path -Recurse -Force -Directory |
- Where-Object { (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }).Count -eq 0 } |
- Sort-Object -Property FullName -Descending |
- ForEach-Object {
- try {
- Remove-Item -Path $_.FullName -Force -Recurse
- } catch {}
- }
- $newSize = Get-FolderSize -Path $path
- $reclaimed = $initialSize - $newSize
- $totalReclaimed += $reclaimed
- Write-ColorOutput " Space reclaimed: $(Format-FileSize -Size $reclaimed)" -ForegroundColor Green
- }
- else {
- Write-ColorOutput " Folder not found" -ForegroundColor Yellow
- }
- }
- Write-Progress -Activity "Cleaning Temp Folders" -Completed
- return $totalReclaimed
- }
- # Function to clean Windows Update cache
- function Clear-WindowsUpdateCache {
- Write-ColorOutput "Cleaning Windows Update Cache..." -ForegroundColor Cyan
- try {
- # Stop Windows Update service
- Stop-Service -Name wuauserv -Force
- $updatePath = "$env:windir\SoftwareDistribution\Download"
- $initialSize = Get-FolderSize -Path $updatePath
- Write-ColorOutput " Size before cleanup: $(Format-FileSize -Size $initialSize)" -ForegroundColor Gray
- # Delete contents
- Get-ChildItem -Path "$updatePath\*" -Recurse -Force | ForEach-Object {
- try {
- Remove-Item -Path $_.FullName -Force -Recurse
- Write-Progress -Activity "Cleaning Windows Update Cache" -Status "Removing $($_.Name)" -PercentComplete -1
- } catch {}
- }
- # Restart service
- Start-Service -Name wuauserv
- $newSize = Get-FolderSize -Path $updatePath
- $reclaimed = $initialSize - $newSize
- Write-ColorOutput " Space reclaimed: $(Format-FileSize -Size $reclaimed)" -ForegroundColor Green
- Write-Progress -Activity "Cleaning Windows Update Cache" -Completed
- return $reclaimed
- } catch {
- Write-ColorOutput " Error cleaning Windows Update Cache" -ForegroundColor Yellow
- return 0
- }
- }
- # Function to clean browser caches
- function Clear-BrowserCaches {
- Write-ColorOutput "Cleaning Browser Caches..." -ForegroundColor Cyan
- $totalReclaimed = 0
- # Chrome
- $chromeCachePath = "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache"
- if (Test-Path -Path $chromeCachePath) {
- $initialSize = Get-FolderSize -Path $chromeCachePath
- Write-ColorOutput " Chrome Cache: $(Format-FileSize -Size $initialSize)" -ForegroundColor Gray
- Get-ChildItem -Path $chromeCachePath -Recurse -Force | ForEach-Object {
- try {
- Remove-Item -Path $_.FullName -Force -Recurse
- Write-Progress -Activity "Cleaning Chrome Cache" -Status "Removing $($_.Name)" -PercentComplete -1
- } catch {}
- }
- $newSize = Get-FolderSize -Path $chromeCachePath
- $reclaimed = $initialSize - $newSize
- $totalReclaimed += $reclaimed
- Write-ColorOutput " Chrome space reclaimed: $(Format-FileSize -Size $reclaimed)" -ForegroundColor Green
- }
- # Firefox
- $firefoxProfiles = "$env:APPDATA\Mozilla\Firefox\Profiles"
- if (Test-Path -Path $firefoxProfiles) {
- $profiles = Get-ChildItem -Path $firefoxProfiles -Directory
- foreach ($profile in $profiles) {
- $cachePath = Join-Path -Path $profile.FullName -ChildPath "cache2"
- if (Test-Path -Path $cachePath) {
- $initialSize = Get-FolderSize -Path $cachePath
- Write-ColorOutput " Firefox Cache: $(Format-FileSize -Size $initialSize)" -ForegroundColor Gray
- Get-ChildItem -Path $cachePath -Recurse -Force | ForEach-Object {
- try {
- Remove-Item -Path $_.FullName -Force -Recurse
- Write-Progress -Activity "Cleaning Firefox Cache" -Status "Removing $($_.Name)" -PercentComplete -1
- } catch {}
- }
- $newSize = Get-FolderSize -Path $cachePath
- $reclaimed = $initialSize - $newSize
- $totalReclaimed += $reclaimed
- Write-ColorOutput " Firefox space reclaimed: $(Format-FileSize -Size $reclaimed)" -ForegroundColor Green
- }
- }
- }
- # Edge
- $edgeCachePath = "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache"
- if (Test-Path -Path $edgeCachePath) {
- $initialSize = Get-FolderSize -Path $edgeCachePath
- Write-ColorOutput " Edge Cache: $(Format-FileSize -Size $initialSize)" -ForegroundColor Gray
- Get-ChildItem -Path $edgeCachePath -Recurse -Force | ForEach-Object {
- try {
- Remove-Item -Path $_.FullName -Force -Recurse
- Write-Progress -Activity "Cleaning Edge Cache" -Status "Removing $($_.Name)" -PercentComplete -1
- } catch {}
- }
- $newSize = Get-FolderSize -Path $edgeCachePath
- $reclaimed = $initialSize - $newSize
- $totalReclaimed += $reclaimed
- Write-ColorOutput " Edge space reclaimed: $(Format-FileSize -Size $reclaimed)" -ForegroundColor Green
- }
- # Brave
- $braveCachePath = "$env:LOCALAPPDATA\BraveSoftware\Brave-Browser\User Data\Default\Cache"
- if (Test-Path -Path $braveCachePath) {
- $initialSize = Get-FolderSize -Path $braveCachePath
- Write-ColorOutput " Brave Cache: $(Format-FileSize -Size $initialSize)" -ForegroundColor Gray
- Get-ChildItem -Path $braveCachePath -Recurse -Force | ForEach-Object {
- try {
- Remove-Item -Path $_.FullName -Force -Recurse
- Write-Progress -Activity "Cleaning Brave Cache" -Status "Removing $($_.Name)" -PercentComplete -1
- } catch {}
- }
- $newSize = Get-FolderSize -Path $braveCachePath
- $reclaimed = $initialSize - $newSize
- $totalReclaimed += $reclaimed
- Write-ColorOutput " Brave space reclaimed: $(Format-FileSize -Size $reclaimed)" -ForegroundColor Green
- }
- Write-Progress -Activity "Cleaning Browser Caches" -Completed
- return $totalReclaimed
- }
- # Function to empty Recycle Bin
- function Empty-RecycleBin {
- Write-ColorOutput "Emptying Recycle Bin..." -ForegroundColor Cyan
- try {
- # Get Recycle Bin size
- $shell = New-Object -ComObject Shell.Application
- $recycleBin = $shell.Namespace(0xA)
- $recycleBinSize = 0
- foreach ($item in $recycleBin.Items()) {
- $recycleBinSize += $item.Size
- }
- Write-ColorOutput " Recycle Bin size: $(Format-FileSize -Size $recycleBinSize)" -ForegroundColor Gray
- # Empty Recycle Bin
- Write-Progress -Activity "Emptying Recycle Bin" -Status "Please wait..." -PercentComplete 50
- Clear-RecycleBin -Force
- Write-ColorOutput " Space reclaimed: $(Format-FileSize -Size $recycleBinSize)" -ForegroundColor Green
- Write-Progress -Activity "Emptying Recycle Bin" -Completed
- return $recycleBinSize
- } catch {
- Write-ColorOutput " Error emptying Recycle Bin" -ForegroundColor Yellow
- return 0
- }
- }
- # Function to clean up old Windows versions (Windows.old)
- function Remove-OldWindowsVersions {
- Write-ColorOutput "Cleaning Old Windows Versions..." -ForegroundColor Cyan
- try {
- $oldWindowsPath = "$env:SystemDrive\Windows.old"
- if (Test-Path -Path $oldWindowsPath) {
- $initialSize = Get-FolderSize -Path $oldWindowsPath
- Write-ColorOutput " Windows.old size: $(Format-FileSize -Size $initialSize)" -ForegroundColor Gray
- # Run disk cleanup tool
- Write-Progress -Activity "Running Disk Cleanup" -Status "Please wait..." -PercentComplete 50
- Start-Process -FilePath "cleanmgr.exe" -ArgumentList "/sagerun:65535" -Wait
- if (Test-Path -Path $oldWindowsPath) {
- $newSize = Get-FolderSize -Path $oldWindowsPath
- $reclaimed = $initialSize - $newSize
- Write-ColorOutput " Space reclaimed: $(Format-FileSize -Size $reclaimed)" -ForegroundColor Green
- Write-Progress -Activity "Cleaning Old Windows Versions" -Completed
- return $reclaimed
- } else {
- Write-ColorOutput " Space reclaimed: $(Format-FileSize -Size $initialSize)" -ForegroundColor Green
- Write-Progress -Activity "Cleaning Old Windows Versions" -Completed
- return $initialSize
- }
- } else {
- Write-ColorOutput " No old Windows installation found" -ForegroundColor Gray
- return 0
- }
- } catch {
- Write-ColorOutput " Error cleaning old Windows versions" -ForegroundColor Yellow
- return 0
- }
- }
- # Function to clean Windows Event Logs
- function Clear-EventLogs {
- Write-ColorOutput "Clearing Windows Event Logs..." -ForegroundColor Cyan
- try {
- $eventLogs = Get-WinEvent -ListLog * -ErrorAction SilentlyContinue | Where-Object { $_.RecordCount -gt 0 }
- $totalSize = 0
- $i = 0
- foreach ($log in $eventLogs) {
- $i++
- $percentage = [math]::Min(100, ($i / $eventLogs.Count) * 100)
- try {
- Write-Progress -Activity "Clearing Event Logs" -Status "Processing $($log.LogName)" -PercentComplete $percentage
- # Get log file path and size
- $logName = $log.LogName
- $logPath = "$env:windir\System32\winevt\Logs\$($logName -replace "/", "-").evtx"
- if (Test-Path -Path $logPath) {
- $logSize = (Get-Item -Path $logPath).Length
- $totalSize += $logSize
- # Clear log if it's large enough
- if ($logSize -gt 1MB) {
- [System.Diagnostics.Eventing.Reader.EventLogSession]::GlobalSession.ClearLog($logName)
- Write-ColorOutput " Cleared log: $($logName -replace "/", "-")" -ForegroundColor Gray
- }
- }
- } catch {}
- }
- Write-ColorOutput " Space reclaimed: $(Format-FileSize -Size $totalSize)" -ForegroundColor Green
- Write-Progress -Activity "Clearing Event Logs" -Completed
- return $totalSize
- } catch {
- Write-ColorOutput " Error clearing event logs" -ForegroundColor Yellow
- return 0
- }
- }
- # Function to clean Windows Defender logs and history
- function Clear-DefenderHistory {
- Write-ColorOutput "Cleaning Windows Defender History..." -ForegroundColor Cyan
- try {
- $defenderPath = "$env:ProgramData\Microsoft\Windows Defender\Scans\History"
- if (Test-Path -Path $defenderPath) {
- $initialSize = Get-FolderSize -Path $defenderPath
- Write-ColorOutput " Defender history size: $(Format-FileSize -Size $initialSize)" -ForegroundColor Gray
- # Remove history files
- Get-ChildItem -Path $defenderPath -Recurse -Force | ForEach-Object {
- try {
- Remove-Item -Path $_.FullName -Force -Recurse
- Write-Progress -Activity "Cleaning Defender History" -Status "Removing $($_.Name)" -PercentComplete -1
- } catch {}
- }
- $newSize = Get-FolderSize -Path $defenderPath
- $reclaimed = $initialSize - $newSize
- Write-ColorOutput " Space reclaimed: $(Format-FileSize -Size $reclaimed)" -ForegroundColor Green
- Write-Progress -Activity "Cleaning Defender History" -Completed
- return $reclaimed
- } else {
- Write-ColorOutput " Defender history folder not found" -ForegroundColor Yellow
- return 0
- }
- } catch {
- Write-ColorOutput " Error cleaning Defender history" -ForegroundColor Yellow
- return 0
- }
- }
- # Function to clean Delivery Optimization files
- function Clear-DeliveryOptimization {
- Write-ColorOutput "Cleaning Delivery Optimization Cache..." -ForegroundColor Cyan
- try {
- # Stop Delivery Optimization service
- Stop-Service -Name DoSvc -Force
- $doPath = "$env:windir\ServiceProfiles\NetworkService\AppData\Local\Microsoft\Windows\DeliveryOptimization\Cache"
- if (Test-Path -Path $doPath) {
- $initialSize = Get-FolderSize -Path $doPath
- Write-ColorOutput " Delivery Optimization size: $(Format-FileSize -Size $initialSize)" -ForegroundColor Gray
- # Remove cache files
- Get-ChildItem -Path "$doPath\*" -Recurse -Force | ForEach-Object {
- try {
- Remove-Item -Path $_.FullName -Force -Recurse
- Write-Progress -Activity "Cleaning Delivery Optimization" -Status "Removing $($_.Name)" -PercentComplete -1
- } catch {}
- }
- # Start service
- Start-Service -Name DoSvc
- $newSize = Get-FolderSize -Path $doPath
- $reclaimed = $initialSize - $newSize
- Write-ColorOutput " Space reclaimed: $(Format-FileSize -Size $reclaimed)" -ForegroundColor Green
- Write-Progress -Activity "Cleaning Delivery Optimization" -Completed
- return $reclaimed
- } else {
- # Start service
- Start-Service -Name DoSvc
- Write-ColorOutput " Delivery Optimization folder not found" -ForegroundColor Yellow
- return 0
- }
- } catch {
- # Start service
- Start-Service -Name DoSvc
- Write-ColorOutput " Error cleaning Delivery Optimization" -ForegroundColor Yellow
- return 0
- }
- }
- # Function to clean thumbnails cache
- function Clear-ThumbnailCache {
- Write-ColorOutput "Cleaning Thumbnail Cache..." -ForegroundColor Cyan
- try {
- $thumbCachePath = "$env:LOCALAPPDATA\Microsoft\Windows\Explorer"
- $thumbCacheFiles = Get-ChildItem -Path $thumbCachePath -Filter "thumbcache_*.db" -Force
- $initialSize = 0
- foreach ($file in $thumbCacheFiles) {
- $initialSize += $file.Length
- }
- Write-ColorOutput " Thumbnail cache size: $(Format-FileSize -Size $initialSize)" -ForegroundColor Gray
- # Remove thumbnail cache files
- foreach ($file in $thumbCacheFiles) {
- try {
- Remove-Item -Path $file.FullName -Force
- Write-Progress -Activity "Cleaning Thumbnail Cache" -Status "Removing $($file.Name)" -PercentComplete -1
- } catch {}
- }
- Write-ColorOutput " Space reclaimed: $(Format-FileSize -Size $initialSize)" -ForegroundColor Green
- Write-Progress -Activity "Cleaning Thumbnail Cache" -Completed
- return $initialSize
- } catch {
- Write-ColorOutput " Error cleaning thumbnail cache" -ForegroundColor Yellow
- return 0
- }
- }
- # Function to clean user temporary files
- function Clear-UserTemp {
- Write-ColorOutput "Cleaning User Temp Files..." -ForegroundColor Cyan
- $totalReclaimed = 0
- # Get all user profiles
- $userProfiles = Get-ChildItem -Path "C:\Users" -Directory
- foreach ($profile in $userProfiles) {
- $tempPath = Join-Path -Path $profile.FullName -ChildPath "AppData\Local\Temp"
- if (Test-Path -Path $tempPath) {
- $initialSize = Get-FolderSize -Path $tempPath
- Write-ColorOutput " Temp folder for $($profile.Name): $(Format-FileSize -Size $initialSize)" -ForegroundColor Gray
- # Delete files older than 7 days
- $cutoffDate = (Get-Date).AddDays(-7)
- Get-ChildItem -Path $tempPath -Recurse -Force | Where-Object {
- !$_.PSIsContainer -and $_.LastWriteTime -lt $cutoffDate
- } | ForEach-Object {
- try {
- Remove-Item -Path $_.FullName -Force
- Write-Progress -Activity "Cleaning User Temp Files" -Status "Removing $($_.Name)" -PercentComplete -1
- } catch {}
- }
- # Remove empty folders
- Get-ChildItem -Path $tempPath -Recurse -Force -Directory |
- Where-Object { (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }).Count -eq 0 } |
- Sort-Object -Property FullName -Descending |
- ForEach-Object {
- try {
- Remove-Item -Path $_.FullName -Force -Recurse
- } catch {}
- }
- $newSize = Get-FolderSize -Path $tempPath
- $reclaimed = $initialSize - $newSize
- $totalReclaimed += $reclaimed
- Write-ColorOutput " Space reclaimed for $($profile.Name): $(Format-FileSize -Size $reclaimed)" -ForegroundColor Green
- }
- }
- Write-Progress -Activity "Cleaning User Temp Files" -Completed
- return $totalReclaimed
- }
- # Main script execution
- # Check for admin rights
- $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
- Clear-Host
- Write-ColorOutput "=========================================" -ForegroundColor Cyan
- Write-ColorOutput " JUST DELETE IT v2.0 " -ForegroundColor Cyan
- Write-ColorOutput " Windows System Cleanup Utility " -ForegroundColor Cyan
- Write-ColorOutput "=========================================" -ForegroundColor Cyan
- Write-Output ""
- if (-not $isAdmin) {
- Write-ColorOutput "WARNING: Not running as Administrator. Some cleanup operations may fail." -ForegroundColor Yellow
- Write-ColorOutput "For best results, please run this script as Administrator." -ForegroundColor Yellow
- Write-Output ""
- }
- $startTime = Get-Date
- $totalSpaceReclaimed = 0
- # Get initial free space
- $drive = Get-PSDrive -Name C
- $initialFreeSpace = $drive.Free
- $initialFreeSpaceFormatted = Format-FileSize -Size $initialFreeSpace
- Write-ColorOutput "Initial free space: $initialFreeSpaceFormatted" -ForegroundColor Magenta
- Write-Output ""
- # Run cleanup operations
- $tempSize = Clear-WindowsTemp
- $totalSpaceReclaimed += $tempSize
- Write-Output ""
- $updateSize = Clear-WindowsUpdateCache
- $totalSpaceReclaimed += $updateSize
- Write-Output ""
- $browserSize = Clear-BrowserCaches
- $totalSpaceReclaimed += $browserSize
- Write-Output ""
- $recycleBinSize = Empty-RecycleBin
- $totalSpaceReclaimed += $recycleBinSize
- Write-Output ""
- $oldWindowsSize = Remove-OldWindowsVersions
- $totalSpaceReclaimed += $oldWindowsSize
- Write-Output ""
- $eventLogSize = Clear-EventLogs
- $totalSpaceReclaimed += $eventLogSize
- Write-Output ""
- $defenderSize = Clear-DefenderHistory
- $totalSpaceReclaimed += $defenderSize
- Write-Output ""
- $doSize = Clear-DeliveryOptimization
- $totalSpaceReclaimed += $doSize
- Write-Output ""
- $thumbSize = Clear-ThumbnailCache
- $totalSpaceReclaimed += $thumbSize
- Write-Output ""
- $userTempSize = Clear-UserTemp
- $totalSpaceReclaimed += $userTempSize
- Write-Output ""
- # Get final free space
- $drive = Get-PSDrive -Name C
- $finalFreeSpace = $drive.Free
- $finalFreeSpaceFormatted = Format-FileSize -Size $finalFreeSpace
- $actualReclaimed = $finalFreeSpace - $initialFreeSpace
- $actualReclaimedFormatted = Format-FileSize -Size $actualReclaimed
- $endTime = Get-Date
- $duration = $endTime - $startTime
- $durationFormatted = "{0:hh\:mm\:ss}" -f $duration
- Write-ColorOutput "=========================================" -ForegroundColor Cyan
- Write-ColorOutput " SUMMARY " -ForegroundColor Cyan
- Write-ColorOutput "=========================================" -ForegroundColor Cyan
- Write-Output ""
- Write-ColorOutput "Operation completed in $durationFormatted" -ForegroundColor Magenta
- Write-ColorOutput "Initial free space: $initialFreeSpaceFormatted" -ForegroundColor Magenta
- Write-ColorOutput "Current free space: $finalFreeSpaceFormatted" -ForegroundColor Magenta
- Write-ColorOutput "Total space reclaimed: $actualReclaimedFormatted" -ForegroundColor Green
- Write-Output ""
- Write-ColorOutput "Just Delete It has completed successfully!" -ForegroundColor Cyan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement