OAC Technology
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).ver sion -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\Cu rrentVersi on\Explore r\VolumeCa ches\Activ e Setup Temp Folders' -name StateFlags1227 -ErrorAction SilentlyContinue)) {
set-itemproperty -path 'HKLM:\Software\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Activ e Setup Temp Folders' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Branc hCache' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Downl oaded Program Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Deliv ery Optimization Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Devic e Driver Packages' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Offli ne Pages Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Inter net Cache Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Old ChkDsk Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Previ ous Installations' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Recyc le Bin' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Servi ce Pack Cleanup' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Setup Log Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Syste m error memory dump files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Syste m error minidump files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Tempo rary Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Tempo rary Setup Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Thumb nail Cache' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Updat e Cleanup' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Upgra de Discarded Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\User file versions' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Windo ws Defender' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Windo ws Error Reporting Archive Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Windo ws Error Reporting Queue Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Windo ws Error Reporting System Archive Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Windo ws Error Reporting System Queue Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Windo ws Error Reporting Temp Files' -name StateFlags1227 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Windo ws ESD installation files' -name StateFlags1227 -type DWORD -Value 0
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\ Windows\Cu rrentVersi on\Explore r\VolumeCa ches\Windo ws 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?
#ensure we're running on windows 10 or newer first
if ((Get-CimInstance win32_operatingsystem).ver
#Set StateFlags1227 setting for each item in Windows 10 disk cleanup utility
if (-not (get-itemproperty -path 'HKLM:\Software\Microsoft\
set-itemproperty -path 'HKLM:\Software\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
EXPERT CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, forgot to take out the -WhatIf in line 37; remove that to set the keys.
CleanMgr runs minimized with -Minimized.
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:
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) {}
-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 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\Wi ndows\Curr entVersion \Explorer\ VolumeCach es\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.
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\Wi
ASKER
@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.
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.
ASKER
@McKnife
This script will be run as a one-off and does not need to be pushed through policy.
This script will be run as a one-off and does not need to be pushed through policy.
ASKER
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.