
As you execute the previous command, you will notice that the Disk Cleanup tool will open with all the settings you have previously configured. Type the following command and press Enter: cleanmgr /sageset:11Remember to change 11 to the number you have chosen to store the settings. If you want to add or remove items you want Disk Cleanup to delete automatically, then use these steps: After running the command, the Disk Cleanup tool will open and run immediately, removing all the files you have configured on the tool. Type the following command and press Enter: C:\Windows\system32\cleanmgr.exe /sagerun:11Remember to change 11 to the number you have chosen. Use the Windows key + X keyboard shortcut to open the Power User menu and select Command Prompt (Admin). If (Test-Path $env:SystemRoot\Logs\CBS\DeepClean. Get-Process -Name cleanmgr,dismhost -ErrorAction Silentl圜ontinue | Wait-Process Second wait neccesary as CleanMgr.exe spins off separate processes.' Write-Host 'Waiting for CleanMgr and DismHost processes. Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -WindowStyle Hidden -Wait New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files' -Name StateFlags0001 -Value 2 -PropertyType DWord Write-Host 'Enabling Temporary Files Cleanup.' New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Update Cleanup' -Name StateFlags0001 -Value 2 -PropertyType DWord This is done automatically in Windows 10 via a scheduled task.' Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*' -Name StateFlags0001 -ErrorAction Silentl圜ontinue | Remove-ItemProperty -Name StateFlags0001 -ErrorAction Silentl圜ontinue
Write-Host 'Clearing CleanMgr.exe automation settings.'
However, at some point CleanMgr spawns new processes which are visible and must be waited on separately. You will find the Registry key names in the "VolumeCaches" branch.Īs far as being silent, this script attempts to start CleanMgr.exe in a hidden window. To automate other extensions, create a "StateFlags0001" property in the corresponding Registry key, as done in the New-ItemProperty lines. In this case, it removes temporary files and runs the Update Cleanup extension to purge superseded Service Pack Backup files (Windows 10 now does this automatically via a scheduled task). The following Powershell script automates CleanMgr.exe.