Run Disk Cleanup silently with cleanmgr /d C /sageset:1 once to choose files, then cleanmgr /d C /sagerun:1 from CMD or Start-Process cleanmgr.exe -ArgumentList '/d C /sagerun:1' -WindowStyle Hidden in PowerShell. Poor automation can leave temp files behind, fail on the wrong drive, or hang waiting for a prompt. This article shows the exact CMD and PowerShell command patterns, how to validate them, and how to schedule repeatable cleanup without user input.
Disk Cleanup command basics
Disk Cleanup uses cleanmgr.exe, Windows' built-in cleanup utility. For automation, the key pieces are /d to choose a drive, /sageset to save cleanup options, and /sagerun to replay that saved profile later. A plain launch opens the UI; a saved profile is what makes batch runs repeatable.
What cleanmgr.exe does
cleanmgr.exe targets common junk categories such as temporary files, DirectX shader cache, delivery optimization files, and Windows Update Cleanup. The saved profile controls which categories run at all. If the profile is missing, the run may open a window instead of finishing unattended.
Which cleanup goals need a saved profile?
Any silent or scheduled run should use /sageset first. That step stores the category choices under a profile number such as 1. Then /sagerun:1 reuses those exact choices later, which is the part that makes batch operation practical.
What silent execution can and cannot do
Silent execution can replay a preselected profile without asking questions. It cannot guess which drive or categories you want. If you skip the profile step or run it under the wrong context, Disk Cleanup may still show a UI or process the wrong target.
What you'll need
- An elevated Command Prompt or PowerShell window.
- Admin rights on the device, or a scheduled task set to run with highest privileges.
- The target drive letter, such as C or D.
- A saved cleanup profile number, such as 1, for silent replay.
- Optional access to Task Scheduler for unattended runs.
Cleanup command matrix
This table maps the most common cleanup goals to exact commands, the elevation they need, whether they stay silent, and the safest check before you schedule them.
| Cleanup goal | CMD command | PowerShell command | Elevation | Silent? | Safest validation step |
|---|---|---|---|---|---|
| Open Disk Cleanup for a drive | cleanmgr /d C | Start-Process cleanmgr.exe -ArgumentList '/d C' | Recommended | No | Confirm the correct drive letter in File Explorer first. |
| Save cleanup choices | cleanmgr /d C /sageset:1 | Start-Process cleanmgr.exe -ArgumentList '/d C /sageset:1' | Yes | No | Review the checkbox list before clicking OK. |
| Run saved profile silently | cleanmgr /d C /sagerun:1 | Start-Process cleanmgr.exe -ArgumentList '/d C /sagerun:1' -WindowStyle Hidden | Yes | Yes | Verify the profile number matches the saved sageset value. |
| Run Windows Update Cleanup on system drive | cleanmgr /d C /sageset:1, then cleanmgr /d C /sagerun:1 | Start-Process cleanmgr.exe -ArgumentList '/d C /sagerun:1' -WindowStyle Hidden | Yes | Yes | Check that Windows Update servicing is complete and a reboot is not pending. |
How do you run Disk Cleanup from command prompt?
Open an elevated cmd.exe window and run cleanmgr /d C for the interactive tool or cleanmgr /d C /sagerun:1 for a saved profile. The /d switch selects the drive, and /sagerun replays the profile number you saved earlier with /sageset.
- Open an elevated prompt. Search for cmd, then choose Run as administrator. Disk Cleanup can read system areas only when it has the right context.
- Save the profile first. Run cleanmgr /d C /sageset:1 and pick the categories you want. Use the same profile number every time.
- Replay the profile. Run cleanmgr /d C /sagerun:1 to execute the saved choices without reselecting them.
Say your goal is to clear old temporary setup files from a lab PC without opening the Disk Cleanup window. Save the profile with /sageset:1, make sure Windows Update Cleanup is checked, then replay the same profile with /sagerun:1. That avoids a one-off run that misses the biggest reclaimable item.
How do you run disk cleanup from PowerShell?
Use PowerShell when you want direct control over how the process starts, whether the window stays visible, and what gets logged. The simplest direct launch is Start-Process cleanmgr.exe -ArgumentList '/d C'. For a silent replay, use Start-Process cleanmgr.exe -ArgumentList '/d C /sagerun:1' -WindowStyle Hidden.
- Start PowerShell elevated. Open Windows Terminal or PowerShell with admin rights so the cleanup can access protected items.
- Create the profile once. Run Start-Process cleanmgr.exe -ArgumentList '/d C /sageset:1' and select the cleanup categories you want to reuse.
- Hide the replay window. Run Start-Process cleanmgr.exe -ArgumentList '/d C /sagerun:1' -WindowStyle Hidden -Wait. Add -Wait if a script needs to stop until cleanup finishes.
- Capture exit status. Check $LASTEXITCODE after the process ends if your job needs a basic success check.
Suppose a maintenance script runs at 2:00 a.m. and must finish before a patch window starts. -Wait keeps the script in order, while -WindowStyle Hidden prevents a visible prompt that could confuse remote users.
Can you automate Disk Cleanup with Task Scheduler?
Yes. Task Scheduler is the usual way to run cleanmgr.exe unattended on a schedule. Create a task that runs with highest privileges, point it at the saved /sagerun command, and confirm the task uses the correct account and drive letter before you let it repeat.
- Create a new task. Open Task Scheduler, choose Create Task, and select Run with highest privileges.
- Set the trigger. Pick a maintenance time that fits your patch cycle, such as weekly after business hours.
- Enter the action. Use Program/script: cleanmgr.exe and Add arguments: /d C /sagerun:1.
- Test the task. Run it once manually, then inspect the Last Run Result field and Event Viewer if it fails.
Before scheduling, confirm the profile number, the drive letter, and the account context. A task running under the wrong user can miss categories that were saved under another profile, or it can open a UI instead of staying unattended.
Why does Disk Cleanup still open a window?
Disk Cleanup still shows a window when the command is incomplete, the saved profile does not exist, or the task is not running in an elevated context. A visible UI can also appear if you run /sageset instead of /sagerun. Silent replay only works after the profile has been created.
- Check the profile number. Make sure /sagerun:1 matches the same number used in /sageset:1.
- Check elevation. Rerun the command from an administrator prompt or set the scheduled task to highest privileges.
- Check the drive. Verify the /d letter is the intended volume, especially on systems with multiple data drives.
- Check the command. Use the full pattern cleanmgr /d C /sagerun:1 or the PowerShell equivalent with Start-Process.
A common first-timer mistake is saving the profile for C: and then replaying the task against D:. The task can still run, but it will not clean the volume you expected.
Common mistakes and fixes
Most failures come from a short list: wrong drive letter, missing profile, or the wrong security context. Disk Cleanup is simple at the surface, but unattended runs depend on exact command syntax and a saved profile created beforehand.
- Wrong drive target. Use /d with a drive letter, such as /d C, and confirm that letter before you schedule anything.
- No saved profile. Run /sageset:1 first, then use the same number with /sagerun:1.
- Not elevated. Open CMD or PowerShell as administrator, or set the scheduled task to run with highest privileges.
- Windows Update files not removed. Recheck that Windows Update Cleanup was selected in the saved profile, and retry after any pending reboot is complete.
- Script exits too early. In PowerShell, add -Wait to Start-Process so the cleanup finishes before later commands run.
Keep one profile number for one drive. That habit makes validation fast and avoids replaying the wrong category set on the wrong volume.
Frequently asked questions
What command runs Disk Cleanup silently?
The silent pattern is cleanmgr /d C /sagerun:1 after you have saved the profile with cleanmgr /d C /sageset:1. In PowerShell, the equivalent is Start-Process cleanmgr.exe -ArgumentList '/d C /sagerun:1' -WindowStyle Hidden.
Which command-line switches work with cleanmgr.exe?
The key switches here are /d to choose the target drive, /sageset to save options, and /sagerun to replay them. For example, cleanmgr /d C /sageset:1 saves the profile, and cleanmgr /d C /sagerun:1 runs it.
Is Disk Cleanup available on Windows 11 and server editions?
Yes, cleanmgr.exe remains available on many Windows 11 and Windows Server installations, though some editions and servicing states can differ. If the tool is present, use the same /sageset and /sagerun pattern; if it is missing, check the installed feature set for that build.