Cleaning Up Windows Without Interaction



To reduce the size of some of my virtual machines, I often run the Windows cleanup tool to get rid of update artifacts and temporary files. While the cleanmgr command has some undocumented options such as /setup, /autoclean and /verylowdisk, I could not achive what I wanted with any combination of these: I wanted to have one command that simply cleans _everything_ without interaction. TL;DR: Put this in a batch file:
@echo off
set rootkey=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches
for /f "tokens=*" %%K in ('reg query %rootkey%') do >NUL REG add "%%K" /v StateFlags0000 /t REG_DWORD /d 2 /f
cleanmgr /sagerun:0
Essentially, this script manually creates the registry keys that would be created by a call to cleanmgr /sageset:0 and checking all the boxes. It then runs cleanmgr /sagerun:0 which non-interactively calls cleanmgr performing every cleanup task available. Remember to run this as an administrator to remove Windows update artifacts. Here's the same thing in PowerShell in case you care:
$vol = Get-Item HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches 
$vol.GetSubKeyNames() | ForEach-Object { $vol.OpenSubKey($_, $true).SetValue('StateFlags0000', 2) }
cleanmgr /SAGERUN:0

5 Replies to “Cleaning Up Windows Without Interaction”

  1. This is exactly what I was looking for. Went through the same hoops as you I'm sure wondering why the verylowdisk check doesn't auto check every box. (Which is what every forum says it does) The lowdisk switch DOES check all the boxes but requires user input to click 'ok'. Such a hassle. Thanks again!
  2. This is great - you haven't figured out any way to do this silently by chance, have you? The scripts are silent, but I wish it could be done without cleanmgr popping up
    1. Unfortunately, I am not quite sure how to do this (yet). You can use Start-Process -WindowStyle hidden cleanmgr /SAGERUN:0 and the first window will be hidden, but cleanmgr seems to launch additional processes not inheriting the hidden property which will consequently pop up anyway. Currently not sure how to do this properly =(.

Leave a Reply

Your email address will not be published. Required fields are marked *