powershellregistrypowershell-3.0resource-cleanup

Automate process of Disk Cleanup cleanmgr.exe without user intervention


I am developing a powershell script file which shall execute some disk cleanup without user intervention. The user shall not be able to configure anything.

When I run cleanmgr.exe /d c: sageset:1 a popup window appears to select files/folders to be cleaned(cleanup options).

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

Is there a way to specify the cleanup options directly with powerhell/command line(without the need to manually select things to be deleted)?


Solution

  • The only solution I found is to manually set the registry values like this:

    ...

    #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