23

我正在开发一个 powershell 脚本文件,该文件将在没有用户干预的情况下执行一些磁盘清理。用户不能配置任何东西。

当我运行cleanmgr.exe /d c: sageset:1一个弹出窗口时,会出现一个选择要清理的文件/文件夹(清理选项)。

这将创建一个包含带有清理选项的设置的注册表项,然后,您可以运行cleanmgr.exe /sagerun:1它将实际执行清理。

有没有办法直接使用 powerhell/命令行指定清理选项(无需手动选择要删除的东西)?

4

7 回答 7

12

以下 Powershell 脚本自动执行 CleanMgr.exe。在这种情况下,它会删除临时文件并运行更新清理扩展以清除被取代的 Service Pack 备份文件(Windows 10 现在通过计划任务自动执行此操作)。要自动化其他扩展,请在相应的注册表项中创建一个“StateFlags0001”属性,如在 New-ItemProperty 行中所做的那样。您将在“VolumeCaches”分支中找到注册表项名称。

至于保持沉默,此脚本会尝试在隐藏窗口中启动 CleanMgr.exe。但是,在某些时候 CleanMgr 会产生新的进程,这些进程是可见的并且必须单独等待。

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....'
}
于 2016-02-05T00:18:38.150 回答
5

您可以使用cleanmgr /verylowdisk静默方式自动执行所有清理步骤。

于 2018-07-03T21:24:56.073 回答
4

我找到的唯一解决方案是手动设置注册表值,如下所示:

...

#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

...

查看完整示例

于 2015-04-15T09:17:31.020 回答
4

下面提供的 PowerShell 逻辑是动态的,可随时使用或自动化,sageset所有选项均已选中,无需用户交互。这是受到这篇文章的多个答案和评论的启发。

注意:我已经根据自己的需要进行了调整,并且在多个远程和本地 Windows 10 系统上成功使用并没有任何问题。

在本地系统上运行

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

在远程系统上运行

$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 

支持资源

于 2020-09-28T16:25:40.027 回答
2

我遇到了同样的问题。研究可能的方法,我发现了以下内容: http ://stealthpuppy.com/cleaning-up-and-reducing-the-size-of-your-master-image/

它展示了如何通过 cmd 创建 sageset 注册表设置。然后,您可以使用 sagerun:# cmd。我还没有通过脚本尝试过它,但已经验证它可以工作......

于 2015-03-20T23:58:03.137 回答
0

此脚本将从注册表中获取所有卷缓存,使它们能够被清理并为所有缓存运行 CLEANMGR.EXE。

$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
于 2019-02-01T02:05:25.090 回答
0

只要您使用具有本地管理员权限的帐户在本地运行 CleanMgr.exe,在 powershell 脚本中或单独运行它似乎都可以正常工作。但是尝试通过任何远程管理工具或远程脚本命令(Invoke-Command)远程运行它,它不会运行。您可能会看到该进程在远程系统上运行,但它似乎没有清理任何东西,并且该进程永远不会结束。如果有人能够让 cleanmgr.exe 在没有任何用户交互的情况下远程运行,我会很感兴趣。EG ConfigMgr 右键单击​​工具、ConfigMgr 应用程序或 PKG、任务计划程序。

于 2022-02-28T22:33:29.620 回答