Solved

Launch CleanMgr.exe minimized from PowerShell

Posted on 2016-07-29
10
58 Views
1 Endorsement
Last Modified: 2016-08-03
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?
1
Comment
Question by:DataDudes
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 12

Accepted Solution

by:
Dustin Saunders earned 250 total points
ID: 41735036
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.
0
 
LVL 83

Assisted Solution

by:oBdA
oBdA earned 250 total points
ID: 41735067
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

1
 
LVL 2

Author Comment

by:DataDudes
ID: 41735076
@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.
0
 
LVL 83

Expert Comment

by:oBdA
ID: 41735077
Sorry, forgot to take out the -WhatIf in line 37; remove that to set the keys.
CleanMgr runs minimized with -Minimized.
0
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41735081
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

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 83

Expert Comment

by:oBdA
ID: 41735097
-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.
0
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41735126
@oBdA - Ah, didn't see that.  Actually, I didn't know [version] was a type so I'm glad you pointed that out.
0
 
LVL 53

Expert Comment

by:McKnife
ID: 41735192
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.
0
 
LVL 2

Author Comment

by:DataDudes
ID: 41735202
@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.
0
 
LVL 2

Author Comment

by:DataDudes
ID: 41735206
@McKnife
This script will be run as a one-off and does not need to be pushed through policy.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

What to do when Windows Update is not working correctly? What tools can I use to detect the cause of the malfunction problem? What does this numeric error code mean? These and other questions that you have been asking in the past are answered here (…
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now