使用PowerShell静默自动化cleanmgr.exe

时间:2014-06-18 11:44:57

标签: powershell disk

我一直在研究一个应该在磁盘C:小于1gb时静默运行cleanmgr.exe的脚本,它运行良好,但有一件事是我无法实现的......

我想完全默默运行cleanmgr.exe!我不想从磁盘清理gui看到任何东西,甚至连屏幕都没说。

使用sageset和sagerun使其自动化是,但它仍然显示了该过程。

这是我的代码(我知道它有一些关于一些事情的问题,但我在这里专注于静默执行,谢谢!):

if ($freeSpace -le $lowSpace)
{   
   cleanmgr.exe /sagerun:10
   defrag C:
}

else
{
   echo "sorry!"
   pause
}

4 个答案:

答案 0 :(得分:4)

实际上cleanmgr /verylowdisk默默运行。

cleanmgr /verylowdisk就像是cleanmgr /lowdisk的版本,没有用户互动。

答案 1 :(得分:1)

如果在命令提示符中导航到C:\windows\system32\cleanmgr.exe /?,您将看到exe的开关。不幸的是,看起来这个实用程序没有静音开关。

enter image description here

答案 2 :(得分:0)

我发现这样做的最好方法是使用schtasks创建一个Scheduled任务(我构建了一个部署的XML配置)并在创建时运行任务,在设置中它可以设置为以system / admin身份运行(你需要一个碎片整理)和非交互式。我遇到的唯一问题是Windows更新清理组件似乎无法运行。

答案 3 :(得分:0)

所以不确定你的位置,但这是我用来清理我们在部署到VMware之前在Packer中构建的模板。

$strKeyPath   = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches"
$strValueName = "StateFlags0065"
$subkeys      = Get-ChildItem -Path $strKeyPath -Name

ForEach($subkey in $subkeys){
    $null = New-ItemProperty `
        -Path $strKeyPath\$subkey `
        -Name $strValueName `
        -PropertyType DWord `
        -Value 2 `
        -ea SilentlyContinue `
        -wa SilentlyContinue
}

Start-Process cleanmgr `
        -ArgumentList "/sagerun:65" `
        -Wait `
        -NoNewWindow `
        -ErrorAction   SilentlyContinue `
        -WarningAction SilentlyContinue

ForEach($subkey in $subkeys){
    $null = Remove-ItemProperty `
        -Path $strKeyPath\$subkey `
        -Name $strValueName `
        -ea SilentlyContinue `
        -wa SilentlyContinue
}

exit 0 
相关问题