Automation of the cleanmgr.exe disk cleanup process without user intervention

I am developing a powershell script file that should do some disk cleanup without user intervention. The user will not be able to configure anything.

When you run cleanmgr.exe /dc: sageset:1 , a pop-up window appears to select the files / folders to be cleaned (cleaning options).

This will create a registry entry containing the settings with the cleanup options, and after that you can run cleanmgr.exe /sagerun:1 , which will actually do the cleanup.

Is it possible to specify the cleaning options directly using the command line (without having to manually select the things that need to be deleted)?

+9
powershell registry
source share
5 answers

The only solution I found was to manually set the registry values ​​as follows:

...

 #Set StateFlags0012 setting for each item in Windows 8.1 disk cleanup utility if  (-not (get-itemproperty -path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders' -name StateFlags0012 -ErrorAction SilentlyContinue)) { set-itemproperty -path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\BranchCache' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files' -name StateFlags0012 -type DWORD -Value 2 

...

see full example

+2
source share

The following Powershell script automates CleanMgr.exe. In this case, it deletes the temporary files and launches the β€œUpdate Cleanup” extension to clear the backup files with backups (Windows 10 now does this automatically using the scheduled task). To automate other extensions, create the "StateFlags0001" property in the appropriate registry key, as is done in the New-ItemProperty lines. You will find the registry key names in the "VolumeCaches" branch.

As for silence, this script tries to run CleanMgr.exe in a hidden window. However, at some point, CleanMgr creates new processes that are visible and must be waited separately.

 Write-Host 'Clearing CleanMgr.exe automation settings.' Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*' -Name StateFlags0001 -ErrorAction SilentlyContinue | Remove-ItemProperty -Name StateFlags0001 -ErrorAction SilentlyContinue Write-Host 'Enabling Update Cleanup. This is done automatically in Windows 10 via a scheduled task.' New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Update Cleanup' -Name StateFlags0001 -Value 2 -PropertyType DWord Write-Host 'Enabling Temporary Files Cleanup.' New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files' -Name StateFlags0001 -Value 2 -PropertyType DWord Write-Host 'Starting CleanMgr.exe...' Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -WindowStyle Hidden -Wait Write-Host 'Waiting for CleanMgr and DismHost processes. Second wait neccesary as CleanMgr.exe spins off separate processes.' Get-Process -Name cleanmgr,dismhost -ErrorAction SilentlyContinue | Wait-Process $UpdateCleanupSuccessful = $false if (Test-Path $env:SystemRoot\Logs\CBS\DeepClean.log) { $UpdateCleanupSuccessful = Select-String -Path $env:SystemRoot\Logs\CBS\DeepClean.log -Pattern 'Total size of superseded packages:' -Quiet } if ($UpdateCleanupSuccessful) { Write-Host 'Rebooting to complete CleanMgr.exe Update Cleanup....' SHUTDOWN.EXE /r /f /t 0 /c 'Rebooting to complete CleanMgr.exe Update Cleanup....' } 
+7
source share

I ran into the same problem. Studying the possible ways, I found the following: http://stealthpuppy.com/cleaning-up-and-reducing-the-size-of-your-master-image/

Shows how to create sageset registry settings using cmd. Then you can use sagerun: # cmd. I have not tried it with a script yet, but confirmed that it works ...

+2
source share

You can use cleanmgr/verylowdisk to automatically automate all cleaning steps.

+2
source share

This script retrieves all volume caches from the registry, allows you to clear them and run CLEANMGR.EXE for all caches.

 $VolumeCachesRegDir = "hklm:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches" $CacheDirItemNames = Get-ItemProperty "$VolumeCachesRegDir\*" | select -ExpandProperty PSChildName $CacheDirItemNames | %{ $exists = Get-ItemProperty -Path "$VolumeCachesRegDir\$_" -Name "StateFlags6553" -ErrorAction SilentlyContinue If (($exists -ne $null) -and ($exists.Length -ne 0)) { Set-ItemProperty -Path "$VolumeCachesRegDir\$_" -Name StateFlags6553 -Value 2 } else { New-ItemProperty -Path "$VolumeCachesRegDir\$_" -Name StateFlags6553 -Value 0 -PropertyType DWord } } Start-Sleep -Seconds 3 Write-Host 'Running CleanMgr.exe...' Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:65535' -WindowStyle Hidden -PassThru cls 
0
source share

All Articles