Automate process of Disk Cleanup cleanmgr.exe without user intervention

后端 未结 6 2326
不知归路
不知归路 2020-12-29 06:54

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 ru

相关标签:
6条回答
  • 2020-12-29 07:36

    This script will get all the Volume Caches from the Registry, enable them to be cleaned and run the 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 讨论(0)
  • 2020-12-29 07:39

    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

    0 讨论(0)
  • 2020-12-29 07:40

    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....'
    }
    
    0 讨论(0)
  • 2020-12-29 07:53

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

    It shows how to create the sageset registry settings via cmd. You can then use the sagerun:# cmd. I have not tried it via script yet, but have validated that it works...

    0 讨论(0)
  • 2020-12-29 07:54

    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

    0 讨论(0)
  • 2020-12-29 07:58

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

    0 讨论(0)
提交回复
热议问题