Pin*_*ani 11 registry powershell resource-cleanup powershell-3.0
我正在开发一个powershell脚本文件,它将执行一些磁盘清理而无需用户干预.用户无法配置任何内容.
当我运行cleanmgr.exe /d c: sageset:1
弹出窗口时,出现选择要清理的文件/文件夹(清理选项).
这将创建一个包含清理选项设置的注册表项,然后,您可以运行cleanmgr.exe /sagerun:1
实际执行清理的设置.
有没有办法直接用powerhell /命令行指定清理选项(不需要手动选择要删除的东西)?
以下Powershell脚本自动执行CleanMgr.exe.在这种情况下,它会删除临时文件并运行Update Cleanup扩展以清除已取代的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....'
}
Run Code Online (Sandbox Code Playgroud)
下面提供的 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
Run Code Online (Sandbox Code Playgroud)
在远程系统上运行
$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
Run Code Online (Sandbox Code Playgroud)
-AsJob
Run Code Online (Sandbox Code Playgroud)Run the command as a background job on a remote computer. Use this parameter to run commands that take an extensive time to complete.
小智 5
您可以使用cleanmgr /verylowdisk
以静默方式自动执行所有清理步骤。
我找到的唯一解决方案是手动设置注册表值,如下所示:
...
#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
Run Code Online (Sandbox Code Playgroud)
...
归档时间: |
|
查看次数: |
45634 次 |
最近记录: |