Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 878
Pinte Dani Avatar asked Mar 04 '15 11:03

Pinte Dani


People also ask

Can you automate Disk Cleanup?

cleanmgr.exe is designed to clear unnecessary files from your computer's hard disk. You can configure cleanmgr.exe with command-line switches to clean up the files you want. You can then schedule the task to run at a specific time by using the Scheduled Tasks tool.

Does Cleanmgr run automatically?

cleanmgr.exe is Microsoft's automating disk cleanup tool in Windows. Its purpose is to free up disk space on your hard drive. cleanmgr.exe will search and analyze your hard drive for files that are no longer needed, then it automatically removes those files.

Can you run Disk Cleanup silently?

However, cleanmgr.exe does have a way of executing silently/quietly without any user interaction: The /SAGERUN switch. However, you can't simply use /SAGERUN without some setup. Note, in the above command I specified the value 123 but you can actually use any integer from 0 to 65535.


3 Answers

The following Powershell script automates CleanMgr.exe. 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). To automate other extensions, create a "StateFlags0001" property in the corresponding Registry key, as done in the New-ItemProperty lines. You will find the Registry key names in the "VolumeCaches" branch.

As far as being silent, this script attempts to start CleanMgr.exe in a hidden window. However, at some point CleanMgr spawns new processes which are visible and must be waited on 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....'
}
like image 126
Nathan Hartley Avatar answered Oct 17 '22 18:10

Nathan Hartley


You can use cleanmgr /verylowdisk to silently automate all the cleanup steps.

like image 41
Ryan Turcotte Avatar answered Oct 17 '22 20:10

Ryan Turcotte


The PowerShell logic provided below is dynamic and ready for use or automation with the sageset options all being selected and no user interaction being required. This was inspired by multiple answers and comments from this post.

Note: I've adjusted for my needs and used successfully without any issues on multiple remote and local Windows 10 systems in particular.

Run on Local System

Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*' | % {
    New-ItemProperty -Path $_.PSPath -Name StateFlags0001 -Value 2 -PropertyType DWord -Force
   };
Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' ##-WindowStyle Hidden

Run on Remote System

$cred = Get-Credential "domain\administrator";
Invoke-Command -ComputerName "computer004" {
    Process {
        Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*' | % {
            New-ItemProperty -Path $_.PSPath -Name StateFlags0001 -Value 2 -PropertyType DWord -Force
           };
        Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -WindowStyle Hidden
    }
} -AsJob -Credential $cred 

Supporting Resources

  • cleanmgr

  • Invoke-Command

    -AsJob

       Run the command as a background job on a remote computer.
       Use this parameter to run commands that take an extensive time to complete.
    
  • Get-Credential

  • Automate process of Disk Cleanup cleanmgr.exe without user intervention

  • Creating a Disk Cleanup Handler

like image 6
Bitcoin Murderous Maniac Avatar answered Oct 17 '22 19:10

Bitcoin Murderous Maniac