Link to home
Start Free TrialLog in
Avatar of OAC Technology
OAC TechnologyFlag for United States of America

asked on

Launch CleanMgr.exe minimized from PowerShell

Please refer to the following script:

#ensure we're running on windows 10 or newer first
if ((Get-CimInstance win32_operatingsystem).version -ge '10.0.0000') {
 
#Set StateFlags1227 setting for each item in Windows 10 disk cleanup utility
if (-not (get-itemproperty -path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders' -name StateFlags1227 -ErrorAction SilentlyContinue)) {
set-itemproperty -path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\BranchCache' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Delivery Optimization Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Device Driver Packages' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Offline Pages Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Old ChkDsk Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Service Pack Cleanup' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Setup Log Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error memory dump files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error minidump files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Thumbnail Cache' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Update Cleanup' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Upgrade Discarded Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\User file versions' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Defender' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Archive Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Queue Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Archive Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Queue Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Temp Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows ESD installation files' -name StateFlags1227 -type DWORD -Value 0
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Upgrade Log Files' -name StateFlags1227 -type DWORD -Value 2
}
CleanMgr.exe /sagerun:1227
}
exit


CleanMgr.exe runs windowed. I would like it to run silently, but acknowledging that this may not be possible, I would like to run it minimized by default so that, if I were to execute this script remotely, the end user would not encounter any popups. Thoughts?
ASKER CERTIFIED SOLUTION
Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
EXPERT CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of OAC Technology

ASKER

@Dustin Saunders
Unfortunately, the Start-Process method displays CleanMgr.exe while it runs as well. For further troubleshooting, I have also tried running it with psexec.exe with the -d -s switches and confirmed that this also does not resolve the original issue.

@oBdA
Thank you for your input.
Avatar of oBdA
oBdA

Sorry, forgot to take out the -WhatIf in line 37; remove that to set the keys.
CleanMgr runs minimized with -Minimized.
It runs minimized for me (tested on Win7, Win10, Server 2012R2 and 2008R2).  It also runs hidden with the flag, do you get any error or it just pops normally?

oBdA brings up a good point about your version check.  Maybe try something like:

$i = (Get-CimInstance win32_operatingsystem).version
[int]$i = $i.Substring(0,$i.IndexOf("."))
if ($i -ge 10) {}

Open in new window

-Hidden works as well for Windows 8.

Dustin Saunders,
the version check is fixed in my post; if the version string consists of only int32 elements, System.Version can be used, which compares correctly.
@oBdA - Ah, didn't see that.  Actually, I didn't know [version] was a type so I'm glad you pointed that out.
Let me guess, you are trying to setup regular maintenance tasks?
That can also be done by deploying a scheduled task that runs as system account and thus will be invisible.
https://support.microsoft.com/en-us/kb/315246 documents that. You'll see that what Dustin wrote (/sagerun) will only work automatically, if we have used /sageset before.

So to deploy sageset values, simply execute the sageset commands on a test machine, gather the registry keys it set below for example HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders\StateFlagsXXXX (xxxx being the number you choose for /sageset) and deploy those using GPOs together with the task. We do this, so I could explain it more detailed if still needed.
@all
Thanks for your assistance on this issue. While I'm less concerned about the version check (this script was taken from another forum and will be run on dissimilar versions of Windows), I do still encounter the CleanMgr popup to display that it is scanning and running. After it runs, the user isn't prompted for any input and the popup disappears, but I would prefer that the display of scanning and running weren't visible to the user at all. No error messages are produced.

Additional detail: I am running Windows 10 x64 as a standard user, launching PowerShell as a local admin before running this script. When I push this script to end users, it will run as a local administrator.
@McKnife
This script will be run as a one-off and does not need to be pushed through policy.