This PowerShell script helps free up disk space and improve system performance on Windows by running various cleanup tasks. Each task is controlled by a toggle flag at the top of the script.
This script can perform the following cleanup actions:
- ποΈ Temporary Files β Cleans TEMP folders (User and System)
- β»οΈ Recycle Bin β Empties the recycle bin
- π§Ή Windows Update Cache β Removes downloaded update files
- πͺ Windows.old β Deletes leftover files from old Windows installations
- β‘ Prefetch β Deletes prefetch data older than 30 days
- π Delivery Optimization β Clears cached delivery optimization files
- πΏ BranchCache β Clears cached network files
- π€ Hibernation File β Disables hibernation and removes
hiberfil.sys - π DNS Cache β Flushes the DNS resolver cache
- π Log Files β Deletes IIS and system log files
- ποΈ Component Store β Cleans up the WinSxS folder using DISM
- π€ Old User Profiles β Deletes user profiles not used for 180+ days
- π§° Disk Cleanup Tool β Executes
cleanmgr /sagerun:1 - π Disk Report β Logs free and total space for each disk
- Log Folder:
C:\temp\Disk_Space_Cleanup - Log Format:
MM-dd-yyyy_Disk_Space_Cleanup.log - Each step logs a timestamped entry indicating:
StartingCompletedFailed
The script is controlled by string flags at the top of the file:
$DoTemp = "True"
$DoWindowsUpdate = "True"
$DoWindowsOld = "True"
$DoRecycleBin = "True"
$DoPrefetch = "True"
$DoDeliveryOptimization = "True"
$DoBranchCache = "True"
$DoHibernation = "True"
$DoFlushDNS = "True"
$DoIISLogs = "True"
$DoComponentCleanup = "True"
$DoOldUserProfiles = "True"
$DoDiskReport = "True"
$DoDiskCleanupTool = "True"| Flag | Description |
|---|---|
$DoTemp |
Clean TEMP folders |
$DoWindowsUpdate |
Clear Windows Update cache |
$DoWindowsOld |
Remove Windows.old and reset component store |
$DoRecycleBin |
Empty the recycle bin |
$DoPrefetch |
Delete old prefetch files |
$DoDeliveryOptimization |
Clear Delivery Optimization cache |
$DoBranchCache |
Clear BranchCache |
$DoHibernation |
Disable hibernation and remove hiberfil.sys |
$DoFlushDNS |
Flush DNS resolver cache |
$DoIISLogs |
Remove IIS and system logs |
$DoComponentCleanup |
DISM component store cleanup |
$DoOldUserProfiles |
Delete profiles not used in 180+ days |
$DoDiskCleanupTool |
Run cleanmgr /sagerun:1 |
$DoDiskReport |
Log disk free/total space to the cleanup log |
π‘οΈ Disclaimer This script is provided βas-isβ without warranty. Please review and test in a safe environment before deploying in production.
Use at your own risk.