I was wondering what other folks are doing in a domain environment to disk cleanup and defrag across all the domain computers on the network. I’ve been looking into using cleanmgr from the command line, but from what I understand you need to run it once with /sageset to setup a preset before you can run it subsequently unattended. is there a way around this?

9 Spice ups

As a precautionary word, you don’t want to defrag your systems if they are using SSD’s.

Cleaning up unused files is one thing, but consumer (not enterprise SSDs) don’t do so well with the abuse performed by a system defrag.

1 Spice up

There is a way around this. You can set the Sage values in the registry first, and then set a scheduled task with /sagerun: to run with those options.

The Registry entry you need is

Type: REG_DWORD
Name: StateFlags (example: StateFlags0100 )
Value: 0x2

All the keys for CleanMgr are located under “HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches”

These are the ones I set to auto-clean every two weeks, but there are many more subkeys available:

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Memory Dump Files
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Old ChkDsk Files
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Service Pack Cleanup
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Setup Log Files
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error memory dump files
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error minidump files
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Thumbnail Cache
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Update Cleanup
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Upgrade Discarded Files
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Archive Files
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Queue Files
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Archive Files
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Queue Files
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Upgrade Log Files

Running this command through task scheduler will clean up all the options with a StateFlags0100 entry in them
%systemroot%\System32\cleanmgr.exe /sagerun:100

1 Spice up

you can schedule from GPO either a logoff script to run and disk clean up automatically

& "$env:windir\System32\cleanmgr.exe" /sagerun:1 

#Temp + Recycle
$Env:temp | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
"$env:windir\Temp\*" | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
$Recycler = (New-Object -ComObject Shell.Application).NameSpace(0xa)
$Recycler.items() | foreach { rm $_.path -force -recurse }

at once as an admin

$computers = get-Content c:\computers.txt
#or get-adcomputer
foreach ($computer in $computers){
if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
{write-host "cannot reach $computer" -f red}

else {& \\$computer\C$\Windows\System32\cleanmgr.exe /sagerun:1}}

Raxco PerfectDisk

to be honest I rarely bother to run a disk defrag anymore.

the comment about SSD is accurate also. you don’t want to defrag an SSD. It reduces the life span of the disk and gives you no benefit for access speeds

1 Spice up

You can push this .bat flle to delete old profiles off the computers. You can specify the profile names.

@ECHO On
for /f %%a in (‘dir c:\users /b’) do (
rd “c:\users\jago.wu” /s /q
rd “c:\users\jagowuadmin” /s /q
)
exit /b 0

We push out some custom powershell scripts via sccm to defrag and cleanup disk space. Then we use gpo to cleanup old unused profiles.

What a lot of people miss is that the CleanMgr can remove superceded windows updates that are not being used, and otherwise eat drive space. there is no other way to safely remove those.

Another MASSIVE drive space user is the CBS logs located in c:\windows\logs\cbs When a windows update is corrupted for whatever reason, the log files in that folder can eat tens-hundreds of gigs in a week. (I had one user that had 133 GB of CBSPersist log files generated over a month)

1 Spice up

this is like all

#Windows disk cleanup

& C:\Windows\System32\cleanmgr.exe /sagerun:1

#Temp + Recycle
$Env:temp | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
"$env:windir\Temp\*" | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
$Recycler = (New-Object -ComObject Shell.Application).NameSpace(0xa)
$Recycler.items() | foreach { rm $_.path -force -recurse }

# Browsers

 #firefox cleanup

            Remove-Item -path "$env\users\$env:username\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache\*" -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path "$env\users\$env:username\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache\*.*" -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path "$env\users\$env:username\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache2\entries\*.*" -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path "$env\users\$env:username\AppData\Local\Mozilla\Firefox\Profiles\*.default\thumbnails\*" -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path "$env\users\$env:username\AppData\Local\Mozilla\Firefox\Profiles\*.default\cookies.sqlite\*" -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path "$env\users\$env:username\AppData\Local\Mozilla\Firefox\Profiles\*.default\webappsstore.sqlite\*" -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path "$env\users\$env:username\AppData\Local\Mozilla\Firefox\Profiles\*.default\chromeappsstore.sqlite\*" -Recurse -Force -EA SilentlyContinue -Verbose

            #chrome

            Remove-Item -path "$env\users\$env:username\AppData\Local\Google\Chrome\User Data\Default\Cache\*" -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path "$env\users\$env:username\AppData\Local\Google\Chrome\User Data\Default\Cache2\entries\*" -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path "$env\users\$env:username\AppData\Local\Google\Chrome\User Data\Default\Cookies\*" -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path "$env\users\$env:username\AppData\Local\Google\Chrome\User Data\Default\Media Cache\*" -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path "$env\users\$env:username\AppData\Local\Google\Chrome\User Data\Default\Cookies-Journal\*" -Recurse -Force -EA SilentlyContinue -Verbose
            Remove-Item -path "$env\users\$env:username\AppData\Local\Google\Chrome\User Data\Default\ChromeDWriteFontCache\*" -Recurse -Force -EA SilentlyContinue -Verbose

            # Internet Explorer

"$env\users\$env:username\Local\Microsoft\Windows\Temporary Internet Files\Low\*" | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
"$env\users\$env:username\AppData\Roaming\Microsoft\Windows\Cookies\Low\*" | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
"$env\users\$env:username\AppData\Local\Microsoft\Windows\History\Low\*" | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
"$env\users\$env:username\AppData\Local\Temp\Low\*" | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue

#Delete logs

Remove-Item -path "C:\windows\logs\CBS\" -recurse -force

```
Ccleaner
$test=Test-Path "$env:programfiles\CCleaner"
if($test -eq "True"){
$os=(gwmi win32_operatingsystem).osarchitecture
If($os -eq  "64-bit"){
& "c:\Program Files\CCleaner\CCleaner64.exe" "/AUTO"
}

else
{
& "c:\Program Files\CCleaner\CCleaner.exe" "/AUTO"
}
}
   
```

so the problem I’m faced with now is I created two tasks in a GPO to run defrag and cleanmgr, however they never show up on any of the targeted computers. with gpresult I can see it was applied, but the two tasks are nowhere to be found in Task Manager. any suggestions?

Here’s an exact export of my functional GPO, including CleanMgr settings, and the Task Scheduler entries.

Permission needs to be Authenticated Users, OU needs to be both Users and Computers for some reason. I forgot why.

Maint__Disk_Cleanup_-_bi-weekly.pdf (62.4 KB)

hrmm, I’m not sure what’s going on… I’ve edited mine to match yours and also applied it to both Users and Computers, but even after a gpupdate it’s still not showing up on any computers’ Task Scheduler. the registry edits do show up there though

The task scheduler items should be in the Task Scheduler Library folder. It should show up as the SYSTEM user account as well. Maybe you have another domain policy somewhere preventing scheduled tasks from being created/run as SYSTEM?

1 Spice up

why the need to do this? What is the end goal?

If you have 100 lappy and each wastes 100gb on log files and temp files…you would have 10,000 GB of waste files…and if you do lappy backups…that will reduce a lot of storage required by backup…

BTW…any idea how to delete those temp xlsx, docx in the users application folder (users\appdata\MS\office\excel (sorry I do not have the exact path)…sometime there are lots of “temp” files which were not cleared.