← script library

Automate Disk Cleanup with PowerShell

Self-Healing & RepairAugust 17, 2026

Checks usage on every filesystem drive and, only on the ones above the threshold, clears the temp folder, empties the recycle bin and drops the Windows Update download cache. Scheduled on a server group it keeps the low-disk ticket from ever being opened; run it once by hand when it already has been.

powershell
# - - - - - PowerShell - - - - - #


# Auto Disk Health & Cleanup
# Checks for low disk space and performs cleanup
# (temp, recycle bin, update cache).
#---------------------------------------------------------------

$Threshold = 50  # Disk usage % limit

Get-PSDrive -PSProvider FileSystem | ForEach-Object {
    try {
        $Usage = [math]::Round((($_.Used / ($_.Free + $_.Used)) * 100), 2)
        Write-Host "<WRITE-LOG = ""*Checking $($_.Name): $Usage% used*"">"

        if ($Usage -ge $Threshold) {
            Write-Host "<WRITE-LOG = ""*Drive $($_.Name) exceeds threshold ($Usage%) - cleaning...*"">"

            # Clear temp files
            Remove-Item "$env:TEMP\*" -Recurse -Force -ErrorAction SilentlyContinue

            # Empty recycle bin
            (New-Object -ComObject Shell.Application).NameSpace(0xA).Items() |
                ForEach-Object { Remove-Item $_.Path -Recurse -Force -ErrorAction SilentlyContinue }

            # Clear Windows Update cache
            Stop-Service wuauserv -ErrorAction SilentlyContinue
            Remove-Item "C:\Windows\SoftwareDistribution\Download\*" -Recurse -Force -ErrorAction SilentlyContinue
            Start-Service wuauserv -ErrorAction SilentlyContinue

            Write-Host "<WRITE-LOG = ""*Cleanup completed for $($_.Name)*"">"
        }
    } catch {
        Write-Host "Error processing $($_.Name): $_"
    }
}

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.