Automate Windows Disk Cleanup with PowerShell
Automate Windows cleanup by choosing the intended data scope, configuring the cleanup profile and checking completion. Keep component servicing separate from user storage management.
Choose the cleanup scope
Disk Cleanup selections, Windows component-store servicing and user-profile cleanup are different tasks. Running a command as SYSTEM does not prove it cleans every user’s files. Decide what may be removed, save work and measure free space before maintenance.
Configure a Disk Cleanup profile
From an elevated Windows terminal, open the selection dialog and review each category:
cleanmgr.exe /sageset:1
The profile number identifies saved settings. Keep its meaning in the maintenance record instead of assuming profile 1 has the same selections on every computer. Categories can include data users still want, so review the dialog on the target system.
Run that configured profile in the intended context:
cleanmgr.exe /sagerun:1
For scheduled maintenance, configure an approved Task Scheduler job with the required account and privileges. Test the same execution context, inspect task results and logs, and prevent overlapping runs. Do not use a fixed fifteen-minute sleep as proof that cleanup completed.
Inspect the component store separately
DISM can analyze Windows component-store size and run supported servicing cleanup:
DISM.exe /Online /Cleanup-Image /AnalyzeComponentStore
DISM.exe /Online /Cleanup-Image /StartComponentCleanup
Run these in an elevated session during an appropriate window. Check the result and servicing logs; an automation wrapper must handle failure rather than print unconditional success. A component cleanup does not remove arbitrary Downloads folders or all users’ temporary files.
The earlier default included /ResetBase. That option has been removed from routine cleanup here because Microsoft’s documentation explains that installed update packages cannot be uninstalled after the operation completes. It requires a separate, deliberate servicing decision.
Check the outcome
Compare free space, review errors and confirm normal operation after any required restart. Retain enough diagnostic and recovery data for support. For user-storage policy, consider Storage Sense settings separately and verify how cloud-backed files and offline availability should behave.
Never manually delete the WinSxS directory to recover space. Use the supported servicing tools and maintain an independent recovery plan for the system.
Practical checks and next steps
Use measured free space and servicing results to decide whether cleanup helped. Keep logs and a maintenance record so repeated cleanup does not hide an underlying capacity problem.
Explore PowerShell & Automation for related guides.
Content review: November 24, 2026. Reviewed the stated scope, guidance and source references; tenant, hardware and production changes were not executed as part of this editorial review. Original publication date retained.
Did this instead of the static 900 seconds sleep:
# wait for task to finish
write-host -nonewline “Waiting for task scheduler to finish: ”
$running = $true
while($running) {
$state = (Get-ScheduledTask -TaskName $taskName).State
if($state -ne “Running”) {
$running = $false
}
write-host -nonewline “*”
sleep 10
}
write-host “”
write-host “Finished!”