自动执行磁盘清理 cleanmgr.exe 过程,无需用户干预

问题描述 投票:0回答:8

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

当我运行

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

这将创建一个注册表项,其中包含带有清理选项的设置,在此之后,您可以运行

cleanmgr.exe /sagerun:1
,这将实际执行清理。

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

powershell registry powershell-3.0 resource-cleanup
8个回答
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....'
}

6
投票

下面提供的 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 

配套资源


5
投票

您可以使用

cleanmgr /verylowdisk
自动执行所有清理步骤。


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

...

查看完整示例


2
投票

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

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


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

0
投票

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


0
投票

我已经尝试在 Windows 10 和 11 测试虚拟机上使用它,既来自我的 RMM 工具(在系统本地运行脚本),也作为 .PS1 从管理 Powershell 会话在它们两个上本地运行,ExecutionPolicy 设置为旁路。 (注意:我的 RMM 工具以 SYSTEM 身份运行脚本;当我使用 PS1 时,我以系统的本地管理员身份运行)。

在我尝试过的几乎所有系统上,cleanmgr 只是第一次挂起并且永远不会完成。我已经尝试过使用 /sageset /sagerun 设置或仅使用 /verylowdisk 的 cleanmgr。我已经使用了辅助等待命令,或者没有它。当我将其作为 .PS1、Cleanmgr 执行时,即使使用 -WindowStyle Hidden 仍然会显示一个弹出窗口,该弹出窗口有时会消失,但脚本会卡住,并在那里停留一个小时或更长时间(测试系统有 SSD,所以不慢),于是我放弃了。当我通过 RMM 工具执行此操作时,它要么像 .PS1 一样挂起,要么偶尔会得到“对象引用未设置为对象的实例”或其他错误的输出。 在某些系统上,如果我结束脚本,它将在连续尝试中运行。但我无法让它始终如一。太令人沮丧了。

我几乎要使用一堆脚本化的 Remove-Item 命令并完全跳过 CLEANMGR.EXE,因为至少这些是有效的。我不确定去哪里。

© www.soinside.com 2019 - 2024. All rights reserved.