Automate process of Disk Cleanup cleanmgr.exe without user intervention

后端 未结 6 2350
不知归路
不知归路 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
    

提交回复
热议问题