Automate process of Disk Cleanup cleanmgr.exe without user intervention

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

提交回复
热议问题