Launch CleanMgr.exe minimized from PowerShell

OAC Technology
OAC Technology used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Co-Founder and Chief Architect
Top Expert 2016
Commented:
Start-Process CleanMgr.exe -ArgumentList "/sagerun:1227" -WindowStyle Minimized

Open in new window


Should work.  You can also use:

Start-Process CleanMgr.exe -ArgumentList "/sagerun:1227" -WindowStyle Hidden

Open in new window


For hidden as originally intended.
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
CAREFUL: This script will not only run on Windows 10, because your comparison is incorrect. You're doing a string comparison, and Windows 8, for example, is '6.3.9600', which is -ge '10.0.0000'.
And you're setting the same thing on a whole bunch of keys, so that's a classic case for a loop.
You can use Minimized or Hidden as WindowsStyle in line 40.
#ensure we're running on windows 10 or newer first
If ([version](Get-CimInstance Win32_OperatingSystem).Version -ge [version]'10.0.0000') {
	$BaseKey = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches'
	If (-not (Get-ItemProperty -Path "$($BaseKey)\Active Setup Temp Folders" -Name StateFlags1227 -ErrorAction SilentlyContinue)) {
		# Set StateFlags1227 setting for each item in Windows 10 disk cleanup utility
		$Keys = @(
			'Active Setup Temp Folders'
			'BranchCache'
			'Downloaded Program Files'
			'Delivery Optimization Files'
			'Device Driver Packages'
			'Offline Pages Files'
			'Internet Cache Files'
			'Old ChkDsk Files'
			'Previous Installations'
			'Recycle Bin'
			'Service Pack Cleanup'
			'Setup Log Files'
			'System error memory dump files'
			'System error minidump files'
			'Temporary Files'
			'Temporary Setup Files'
			'Thumbnail Cache'
			'Update Cleanup'
			'Upgrade Discarded Files'
			'User file versions'
			'Windows Defender'
			'Windows Error Reporting Archive Files'
			'Windows Error Reporting Queue Files'
			'Windows Error Reporting System Archive Files'
			'Windows Error Reporting System Queue Files'
			'Windows Error Reporting Temp Files'
			'Windows ESD installation files'
			'Windows Upgrade Log Files'	
		)
		ForEach ($Key In $Keys) {
			Set-ItemProperty -Path "$($BaseKey)\$($Key)" -Name StateFlags1227 -Type DWORD -Value 2 -WhatIf
		}
	}
	Start-Process cleanmgr.exe -ArgumentList "/sagerun:1227" -WindowStyle Minimized
}
exit
 

Open in new window

OAC TechnologyProfessional Nerds

Author

Commented:
@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.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Sorry, forgot to take out the -WhatIf in line 37; remove that to set the keys.
CleanMgr runs minimized with -Minimized.
Dustin SaundersCo-Founder and Chief Architect
Top Expert 2016

Commented:
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

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
-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.
Dustin SaundersCo-Founder and Chief Architect
Top Expert 2016

Commented:
@oBdA - Ah, didn't see that.  Actually, I didn't know [version] was a type so I'm glad you pointed that out.
Distinguished Expert 2018

Commented:
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.
OAC TechnologyProfessional Nerds

Author

Commented:
@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.
OAC TechnologyProfessional Nerds

Author

Commented:
@McKnife
This script will be run as a one-off and does not need to be pushed through policy.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial