ControlUp users discussed adding the CLEANMGR method to an existing script for cleaning up the Windows Update Cleanup from Disk Cleanup. A script from the CU Script Library may be able to help, with the necessary options to include the desired folders. The Clean Windows System Drive script may also be an option. Get more information about scripts at https://support.controlup.com/docs/add-scripts-from-the-script-library.
Read the entire ‘Adding CLEANMGR to an Existing ControlUp Cleaning Script’ thread below:
Good morning! I hope I’m in the right chat, but I could use some help with a script for clearing out the Windows Update Cleanup from Disk Cleanup
I currently have the below script to clean our HD space and would like to add CLEANMGR to it. OR… maybe it should be a seperate script since it takes so long to run?
cmd.exe /C rd /s C:’$’Recycle.Bin /Q
Remove-Item C:\Temp\CM12CU5* -recurse -Force -ErrorAction SilentlyContinue
Remove-Item C:\Windows\Temp* -recurse -Force -ErrorAction SilentlyContinue
Remove-Item C:\Windows\ccmcache* -recurse -Force -ErrorAction SilentlyContinue
Remove-Item C:\Windows\ccmsetup* -recurse -Force -ErrorAction SilentlyContinue
Remove-Item C:\Windows\SoftwareDistribution\Download* -recurse -Force -ErrorAction SilentlyContinue
This is the info I got off of a CU script called Clean Windows System Drive.
I’m not a script guy so I’m not sure how to use the info below. Any assitance would be appreciated. Thank you.
Keys to set CLEANMGR to clean, remove unwanted entries
[array]$arrSageSetKeys = @(
‘Active Setup Temp Folders’,
‘BranchCache’,
‘Compress System Disk’,
‘Content Indexer Cleaner’,
‘D3D Shader Cache’,
‘Delivery Optimization Files’,
‘Device Driver Packages’,
‘Diagnostic Data Viewer database files’,
‘Downloaded Program Files’,
‘Internet Cache Files’,
‘Offline Pages Files’,
‘Old ChkDsk Files’,
‘Previous Installations’,
‘Recycle Bin’,
‘RetailDemo Offline Content’,
‘Service Pack Cleanup’,
‘Setup Log Files’,
‘System error memory dump files’,
‘System error minidump files’,
‘Temporary Files’,
‘Temporary Setup Files’,
‘Temporary Sync Files’,
‘Thumbnail Cache’,
‘Update Cleanup’,
‘Upgrade Discarded Files’,
‘User file versions’,
‘Users Download Folder’,
‘Windows Defender’,
‘Windows Error Reporting Files’,
‘Windows ESD installation files’,
‘Windows Upgrade Log Files’
)
Hi Troy,
I’m the one who wrote that script. What you are looking at is a list of keys that is set in the registry as a SAGE set, which cleanmgr.exe uses to run silently. It has been a while since I wrote it, but at the time (and I suspect stil) this was the only way to run cleanmgr unattended, so the values have to be written to the registry first. Then I call cleanmgr,exe, referring to that SAGE set, which is read from the registry.
What exactly are you trying to achieve, do you just want to clean the folders in the piece of code you provided with the Remove-Item lines or do you want to do a ‘full clean’ like the Clean Windows System Drive script does?
Hi. Thank you for your response. I would like clean all options or at the very least… the Windows Update Cleanup.
And I’d like to add it to an existing script we use.
cmd.exe /C rd /s C:’$’Recycle.Bin /Q
Remove-Item C:\Temp\CM12CU5* -recurse -Force -ErrorAction SilentlyContinue
Remove-Item C:\Windows\Temp* -recurse -Force -ErrorAction SilentlyContinue
Remove-Item C:\Windows\ccmcache* -recurse -Force -ErrorAction SilentlyContinue
Remove-Item C:\Windows\ccmsetup* -recurse -Force -ErrorAction SilentlyContinue
Remove-Item C:\Windows\SoftwareDistribution\Download* -recurse -Force -ErrorAction SilentlyContinue
Currently we use ControlUp to clean the C: drive folders above, but we then have to log on the VDI manually to run disk cleanup. We’d like to do it all via Control Up.
Yeah, cleanmgr is not installed by default.
Do you have an agent on the VDI?
The CU agent? Yes
Which one though, Edge?
The script was ported to Edge a while back as well, iirc it is more or less the same with a few tweaks.
Whichever Agent you have, Realtiome or Edge, what you would need to do is along these lines:
- In the script in the [array]$arrFoldersToBeCleaned section, add the folders you want cleaned. Temp folders are already in there, if you want onyl the folder c:\temp\CM12CU5 to be cleaned add that part to the line with ‘%systemdrive%\Temp’
- RUn the script through the agent of your choice, but you have to set the correct options. If you just want the list of files in arrFilesToBeDeleted and folders in $arrFoldersToBeCleaned to be cleaned, run it without options. It will go through those lists of files and folders and remove them in more or less the same way as you are doing with your Remove-Item commands
- If you also want to run cleanmgr with the provided set, and pass only ‘True’ as an argument.
- If you also want to to remove the Volume Shadow copies, run the script with arguments True and True (the first is to enable cleanmgr.exe, the second the Shadow Volume)
- The 3rd argument is for DISM fallback, which I cannot imagine you will need for a VDI, so ignore it
Couple of notes: Doublecheck the file list and the SAGE set. Lots of items in there, it is very thorough. Maybe more thorough than you would like, so make sure you are OK with everything that is being removed.
Also, many many Windows updates ago (so it may have been fixed) cleanmgr.exe would often hang when it was done. Not using CPU or disk, just sits there doing nothing. This is not a big problem as the scripts through the agents have a timeout anyway and the process will be killed. Just a heads up if you think Hmmmm that is taking very long
If you are using Edge on the machine, here is the way to add the script to your pwn environment:
https://support.controlup.com/docs/add-scripts-from-the-script-library
Hi. thanks for the good info. We’re not on EDGE DX but Realtime DX. We only monitor our Virtual environment.
So I have to add this, correct?
Run CLEANMGR if required
If ($bolCleanmgr) {
# Is CLEANMGR available on the system?
If (Test-Path ([System.Environment]::ExpandEnvironmentVariables("%systemroot%\System32\cleanmgr.exe"))) {
# Create the SAGESET for CLEANMGR in registry
[string]$strRegPath = ‘HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches’
Foreach ($key in $arrSageSetKeys) {
try {
# Check if the path actually exists before trying to set the key (keys exisitng can depend on OS< patch level etc)
If (Test-Path "$strRegPath\$key") {
New-ItemProperty -Path "$strRegPath\$key" -Name ‘StateFlags0066’ -Value 2 -PropertyType DWORD -Force | Out-Null
}
}
catch {
Feedback "SAGESET registry keys for CLEANMGR could not be set."
}
}
# Run CLEANMGR as a Job, so a time out can be used because cleanmgr sometimes hangs if run silently
try {
# Set timeout
[int]$intTimeOut = 1800
$CodeBlock = {
Start-Process cleanmgr.exe -Wait -ArgumentList ‘/SAGERUN:66’
}
# Start the job
$Job = Start-Job -ScriptBlock $CodeBlock
# Wait for the job to complete
Wait-Job $Job -Timeout $intTimeOut | Out-Null
# Has the job completed?
If ($Job.State -ne ‘Completed’) {
Feedback "CLEANMGR did not complete in the specified time of $intTimeOut seconds. It may have run but failed tot exit. Script will continue."
Stop-Process -Name cleanmgr -Force
}
[array]$arrSageSetKeys = @(
‘Active Setup Temp Folders’,
‘BranchCache’,
‘Compress System Disk’,
‘Content Indexer Cleaner’,
‘D3D Shader Cache’,
‘Delivery Optimization Files’,
‘Device Driver Packages’,
‘Diagnostic Data Viewer database files’,
‘Downloaded Program Files’,
‘Internet Cache Files’,
‘Offline Pages Files’,
‘Old ChkDsk Files’,
‘Previous Installations’,
‘Recycle Bin’,
‘RetailDemo Offline Content’,
‘Service Pack Cleanup’,
‘Setup Log Files’,
‘System error memory dump files’,
‘System error minidump files’,
‘Temporary Files’,
‘Temporary Setup Files’,
‘Temporary Sync Files’,
‘Thumbnail Cache’,
‘Update Cleanup’,
‘Upgrade Discarded Files’,
‘User file versions’,
‘Users Download Folder’,
‘Windows Defender’,
‘Windows Error Reporting Files’,
‘Windows ESD installation files’,
‘Windows Upgrade Log Files’
)
Never mind… let me ready through your notes more carefully. I’ll reply back when understand better what you’re telling me.
Thank you for your help today.
Np.
If you use the script from the Realtime library, it is set up with the options you need to use. Edit the script and add the folders ot the array. Run it on your target and just fill in True or False for the options you want to switch on or off.
If you have any more questions, ping me in your reply so I get a notification 🙂
Thanks Ton. If I don’t reach out later today, have a great weekend.
Continue reading and comment on the thread ‘Adding CLEANMGR to an Existing ControlUp Cleaning Script’. Not a member? Join Here!
Categories: All Archives, ControlUp Edge DX, ControlUp Real-Time DX, ControlUp Scripts & Triggers