23

ユーザーの介入なしでディスクのクリーンアップを実行するpowershellスクリプトファイルを開発しています。ユーザーは何も設定できません。

実行cleanmgr.exe /d c: sageset:1すると、クリーンアップするファイル/フォルダーを選択するためのポップアップ ウィンドウが表示されます (クリーンアップ オプション)。

これにより、クリーンアップ オプションを含む設定を含むレジストリ エントリが作成され、その後、cleanmgr.exe /sagerun:1実際にクリーンアップを実行する実行が可能になります。

powerhell/コマンドラインを使用してクリーンアップオプションを直接指定する方法はありますか(削除するものを手動で選択する必要はありません)?

4

7 に答える 7

12

次の Powershell スクリプトは、CleanMgr.exe を自動化します。この場合、一時ファイルを削除し、Update Cleanup 拡張機能を実行して、置き換えられた Service Pack バックアップ ファイルを消去します (Windows 10 では、スケジュールされたタスクを介してこれが自動的に行われます)。他の拡張機能を自動化するには、New-ItemProperty 行で行ったように、対応するレジストリ キーに "StateFlags0001" プロパティを作成します。レジストリ キーの名前は、「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 に答える
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 に答える