首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在不需要用户干预的情况下自动处理磁盘清理cleanmgr.exe

在不需要用户干预的情况下自动处理磁盘清理cleanmgr.exe
EN

Stack Overflow用户
提问于 2015-03-04 11:01:31
回答 7查看 79.2K关注 0票数 24

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

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

这将创建一个注册表条目,其中包含带有清理选项的设置,在此之后,您可以运行实际执行清理的cleanmgr.exe /sagerun:1

是否有一种方法可以直接使用powerhell/命令行指定清理选项(而不需要手动选择要删除的内容)?

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2015-04-15 09:17:31

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

..。

代码语言:javascript
复制
#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

..。

见完整示例

票数 4
EN

Stack Overflow用户

发布于 2016-02-05 00:18:38

下面的Powershell脚本自动执行CleanMgr.exe。在本例中,它删除临时文件并运行更新清理扩展名以清除已取代的Service备份文件(Windows 10现在通过计划的任务自动完成此操作)。要自动化其他扩展,在相应的Registry中创建一个"StateFlags0001“属性,就像New行中所做的那样。您将在"VolumeCaches“分支中找到注册表密钥名。

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

代码语言:javascript
复制
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....'
}
票数 12
EN

Stack Overflow用户

发布于 2020-09-28 16:25:40

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

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

在本地系统上运行

代码语言:javascript
复制
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

运行在远程系统

代码语言:javascript
复制
$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 

支援资源

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28852786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档