# ============================================================================ # cleanup.drraypc.com - web-run version of Raymond Mayer's cleanup module # # USAGE: # Normal cleanup : irm https://cleanup.drraypc.com | iex # Server cleanup : & ([scriptblock]::Create((irm https://cleanup.drraypc.com))) -Server # # Changes from the original .psm1 (so it survives being piped to iex): # - #Requires directives replaced with an in-code admin check + self-elevation # - PSWindowsUpdate is installed on demand instead of via #Requires # - Export-ModuleMember removed (only valid inside a real module) # - The cleanup function is actually invoked at the bottom # ============================================================================ param([switch]$Server) # must stay the FIRST statement for -Server to bind # --- Self-elevate: relaunch an elevated shell that re-downloads and runs ----- if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Write-Host "Requesting administrator rights..." -ForegroundColor Yellow $srv = if ($Server) { ' -Server' } else { '' } Start-Process powershell.exe -Verb RunAs -ArgumentList @( '-NoProfile','-ExecutionPolicy','Bypass','-Command', "& ([scriptblock]::Create((irm https://cleanup.drraypc.com)))$srv" ) return } # --- Ensure PSWindowsUpdate is available (needed by Install-WindowsUpdate) --- if (-not (Get-Module -ListAvailable -Name PSWindowsUpdate)) { Write-Host "Installing PSWindowsUpdate module..." -ForegroundColor Yellow try { Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force | Out-Null Set-PSRepository -Name PSGallery -InstallationPolicy Trusted -ErrorAction SilentlyContinue Install-Module -Name PSWindowsUpdate -Force -Scope AllUsers -ErrorAction Stop } catch { Write-Host "Could not install PSWindowsUpdate; the Windows Update step will be skipped." -ForegroundColor Red } } Import-Module PSWindowsUpdate -ErrorAction SilentlyContinue # ============================================================================ # ORIGINAL FUNCTION (unchanged logic) # ============================================================================ function cleanup { <# .SYNOPSIS Cleans up temporary files, updates Windows, and optimizes disk. .PARAMETER Server Runs a server-specific cleanup (WinSxS, component store, cleanmgr) without installing Windows Updates or disabling hibernation. #> param( [switch]$server ) if ($server) { # Get System Uptime $uptimeSpan = (Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime Write-Host "`nDevice Uptime: " -NoNewLine Write-Host "$($uptimeSpan.Days)d $($uptimeSpan.Hours)h $($uptimeSpan.Minutes)m`n" -ForegroundColor Green # Get Windows Update download size and remove it $winupdatefiles = "C:\Windows\SoftwareDistribution\Download\*" $winupdatesize = "{0:N2} MB" -f ((Get-ChildItem $winupdatefiles -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB) Remove-Item $winupdatefiles -Recurse -Force -ErrorAction SilentlyContinue Write-Host "[x] " -ForegroundColor Green -NoNewLine Write-Host "$winupdatesize Windows Update Files Removed.`n" # Get Windows Temp size and remove it $wintempfiles = "C:\Windows\Temp\*" $wintempsize = "{0:N2} MB" -f ((Get-ChildItem $wintempfiles -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB) Remove-Item $wintempfiles -Recurse -Force -ErrorAction SilentlyContinue Write-Host "[x] " -ForegroundColor Green -NoNewLine Write-Host "$wintempsize Windows Temp Files Removed.`n" # Clean Windows Component Store dism /online /cleanup-image /analyzecomponentstore dism /online /cleanup-image /startcomponentcleanup dism /online /cleanup-image /startcomponentcleanup /resetbase Write-Host "`n`n[x] " -ForegroundColor Green -NoNewLine Write-Host "Cleaned Windows Component Store`n" cleanmgr Write-Host "[x] " -ForegroundColor Green -NoNewLine Write-Host "Executed Final Disk Cleanup`n`n" return } # MAIN RUN # Get System Uptime $uptimeSpan = (Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime Write-Host "`nDevice Uptime: " -NoNewLine Write-Host "$($uptimeSpan.Days)d $($uptimeSpan.Hours)h $($uptimeSpan.Minutes)m`n" -ForegroundColor Green # Install Windows Update try { Install-WindowsUpdate -AcceptAll -MicrosoftUpdate Write-Host "`n[x] " -ForegroundColor Green -NoNewLine Write-Host "Installed Windows Updates.`n" } catch { Write-Host "[x] " -ForegroundColor Red -NoNewLine Write-Host "Error installing Windows Updates.`n" Write-Host $_ } # Get Windows Update download size and remove it try { $winupdatefiles = "C:\Windows\SoftwareDistribution\Download\*" $winupdatesize = "{0:N2} MB" -f ((Get-ChildItem $winupdatefiles -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB) Remove-Item $winupdatefiles -Force -Recurse -ErrorAction SilentlyContinue Write-Host "[x] " -ForegroundColor Green -NoNewLine Write-Host "$winupdatesize Windows Update Files Removed.`n" } catch { Write-Host "[x] " -ForegroundColor Red -NoNewLine Write-Host "Error removing Windows Update files.`n" } # Get Windows Temp size and remove it try { $wintempfiles = "C:\Windows\Temp\*" $wintempsize = "{0:N2} MB" -f ((Get-ChildItem $wintempfiles -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB) Remove-Item $wintempfiles -Recurse -Force -ErrorAction SilentlyContinue Write-Host "[x] " -ForegroundColor Green -NoNewLine Write-Host "$wintempsize Windows Temp Files Removed.`n" } catch { Write-Host "[x] " -ForegroundColor Red -NoNewLine Write-Host "Error removing Windows Temp files.`n" } # Get Windows dump file size and remove them try { $windmpfiles = Get-ChildItem "C:\Windows" -Include *.dmp, *.hdmp, *.mdmp -Recurse -Force -ErrorAction SilentlyContinue $windmpsize = "{0:N2} MB" -f (($windmpfiles | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB) $windmpfiles | Remove-Item -Force -ErrorAction SilentlyContinue Write-Host "[x] " -ForegroundColor Green -NoNewLine Write-Host "$windmpsize Windows Dump Files Removed.`n" } catch { Write-Host "[x] " -ForegroundColor Red -NoNewLine Write-Host "Error removing Windows Dump files.`n" } # Get User temp file size and remove them try { $tempfolders = @( "C:\Users\*\AppData\Local\Temp\*", "C:\Users\*\AppData\Local\Microsoft\Windows\INetCache\*", "C:\Users\*\AppData\Local\Microsoft\Windows\Temporary Internet Files\*" ) $usertmpfiles = Get-ChildItem $tempfolders -Recurse -Force -ErrorAction SilentlyContinue $usertmpfilesize = "{0:N2} MB" -f (($usertmpfiles | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB) Remove-Item $usertmpfiles -Recurse -Force -ErrorAction SilentlyContinue Write-Host "[x] " -ForegroundColor Green -NoNewLine Write-Host "$usertmpfilesize Temporary User Files Removed.`n" } catch { Write-Host "[x] " -ForegroundColor Red -NoNewLine Write-Host "Error removing Temporary User files.`n" } # Optimize C drive try { Optimize-Volume -DriveLetter C -ReTrim -ErrorAction SilentlyContinue Write-Host "[x] " -ForegroundColor Green -NoNewLine Write-Host "Optimized SSD (ReTrim)`n" } catch { Write-Host "[x] " -ForegroundColor Red -NoNewLine Write-Host "Error optimizing SSD (ReTrim)`n" } # Disable hibernation try { powercfg -h off if ($LASTEXITCODE -ne 0) { throw "powercfg exited with code $LASTEXITCODE" } Write-Host "[x] " -ForegroundColor Green -NoNewLine Write-Host "Disabled Hibernate Feature`n" } catch { Write-Host "[x] " -ForegroundColor Red -NoNewLine Write-Host "Error disabling the Hibernate feature`n" } # Clean/repair Windows Component Store try { dism /online /cleanup-image /analyzecomponentstore if ($LASTEXITCODE -ne 0) { throw "DISM analyzecomponentstore failed with exit code $LASTEXITCODE" } dism /online /cleanup-image /startcomponentcleanup if ($LASTEXITCODE -ne 0) { throw "DISM startcomponentcleanup failed with exit code $LASTEXITCODE" } dism /online /cleanup-image /startcomponentcleanup /resetbase if ($LASTEXITCODE -ne 0) { throw "DISM resetbase failed with exit code $LASTEXITCODE" } Write-Host "`n`n[x] " -ForegroundColor Green -NoNewLine Write-Host "Cleaned Windows Component Store`n" dism /online /cleanup-image /restorehealth if ($LASTEXITCODE -ne 0) { throw "DISM restorehealth failed with exit code $LASTEXITCODE" } Write-Host "`n`n[x] " -ForegroundColor Green -NoNewLine Write-Host "Restored Windows Component Store Health`n" sfc /scannow if ($LASTEXITCODE -ne 0) { throw "SFC scannow failed with exit code $LASTEXITCODE" } Write-Host "`n`n[x] " -ForegroundColor Green -NoNewLine Write-Host "Repaired System Files`n" cleanmgr Write-Host "[x] " -ForegroundColor Green -NoNewLine Write-Host "Executed Final Disk Cleanup`n`n" } catch { Write-Host "[x] " -ForegroundColor Red -NoNewLine Write-Host "Error cleaning/repairing Windows Component Store`n" Write-Host $_ } } # ============================================================================ # RUN IT (this is what makes `| iex` actually do something) # ============================================================================ cleanup -Server:$Server