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?<\/p>","upvoteCount":9,"answerCount":16,"datePublished":"2017-06-29T15:47:05.000Z","author":{"@type":"Person","name":"itnmok","url":"https://community.spiceworks.com/u/itnmok"},"acceptedAnswer":{"@type":"Answer","text":"
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.<\/p>\n
The Registry entry you need is<\/p>\n
Type: REG_DWORD
\nName: StateFlags (example: StateFlags0100 )
\nValue: 0x2<\/p>\n
All the keys for CleanMgr are located under “HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches”<\/p>\n
These are the ones I set to auto-clean every two weeks, but there are many more subkeys available:<\/p>\n
HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Active Setup Temp Folders
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Downloaded Program Files
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Internet Cache Files
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Memory Dump Files
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Old ChkDsk Files
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Previous Installations
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Recycle Bin
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Service Pack Cleanup
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Setup Log Files
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\System error memory dump files
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\System error minidump files
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Temporary Files
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Temporary Setup Files
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Thumbnail Cache
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Update Cleanup
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Upgrade Discarded Files
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Windows Error Reporting Archive Files
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Windows Error Reporting Queue Files
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Windows Error Reporting System Archive Files
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Windows Error Reporting System Queue Files
\nHKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VolumeCaches\\Windows Upgrade Log Files<\/p>\n
Running this command through task scheduler will clean up all the options with a StateFlags0100 entry in them
\n%systemroot%\\System32\\cleanmgr.exe /sagerun:100<\/p>","upvoteCount":1,"datePublished":"2017-06-29T16:03:14.000Z","url":"https://community.spiceworks.com/t/disk-cleanup-across-domain-computers/590620/3","author":{"@type":"Person","name":"CrashFF","url":"https://community.spiceworks.com/u/CrashFF"}},"suggestedAnswer":[{"@type":"Answer","text":"
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?<\/p>","upvoteCount":9,"datePublished":"2017-06-29T15:47:05.000Z","url":"https://community.spiceworks.com/t/disk-cleanup-across-domain-computers/590620/1","author":{"@type":"Person","name":"itnmok","url":"https://community.spiceworks.com/u/itnmok"}},{"@type":"Answer","text":"
As a precautionary word, you don’t want to defrag your systems if they are using SSD’s.<\/p>\n
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.<\/p>","upvoteCount":1,"datePublished":"2017-06-29T15:54:57.000Z","url":"https://community.spiceworks.com/t/disk-cleanup-across-domain-computers/590620/2","author":{"@type":"Person","name":"dbeuerlein","url":"https://community.spiceworks.com/u/dbeuerlein"}},{"@type":"Answer","text":"
you can schedule from GPO either a logoff script to run and disk clean up automatically<\/p>\n
& \"$env:windir\\System32\\cleanmgr.exe\" /sagerun:1 \n\n#Temp + Recycle\n$Env:temp | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue\n\"$env:windir\\Temp\\*\" | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue\n$Recycler = (New-Object -ComObject Shell.Application).NameSpace(0xa)\n$Recycler.items() | foreach { rm $_.path -force -recurse }\n\n<\/code><\/pre>\nat once as an admin<\/p>\n
$computers = get-Content c:\\computers.txt\n#or get-adcomputer\nforeach ($computer in $computers){\nif(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))\n{write-host \"cannot reach $computer\" -f red}\n\nelse {& \\\\$computer\\C$\\Windows\\System32\\cleanmgr.exe /sagerun:1}}\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2017-06-29T16:05:41.000Z","url":"https://community.spiceworks.com/t/disk-cleanup-across-domain-computers/590620/4","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"Raxco PerfectDisk<\/p>","upvoteCount":0,"datePublished":"2017-06-29T16:06:58.000Z","url":"https://community.spiceworks.com/t/disk-cleanup-across-domain-computers/590620/5","author":{"@type":"Person","name":"benkuepers","url":"https://community.spiceworks.com/u/benkuepers"}},{"@type":"Answer","text":"
to be honest I rarely bother to run a disk defrag anymore.<\/p>\n
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<\/p>","upvoteCount":1,"datePublished":"2017-06-29T16:35:58.000Z","url":"https://community.spiceworks.com/t/disk-cleanup-across-domain-computers/590620/6","author":{"@type":"Person","name":"molan","url":"https://community.spiceworks.com/u/molan"}},{"@type":"Answer","text":"
You can push this .bat flle to delete old profiles off the computers. You can specify the profile names.<\/p>\n
@ECHO<\/span> On<\/strong>
\nfor /f %%a in (‘dir c:\\users /b’) do (<\/strong>
\nrd “c:\\users\\jago.wu” /s /q<\/strong>
\nrd “c:\\users\\jagowuadmin” /s /q<\/strong>
\n)<\/strong>
\nexit /b 0<\/strong><\/p>","upvoteCount":0,"datePublished":"2017-06-29T16:47:31.000Z","url":"https://community.spiceworks.com/t/disk-cleanup-across-domain-computers/590620/7","author":{"@type":"Person","name":"jagowu","url":"https://community.spiceworks.com/u/jagowu"}},{"@type":"Answer","text":"We push out some custom powershell scripts via sccm to defrag and cleanup disk space. Then we use gpo to cleanup old unused profiles.<\/p>","upvoteCount":0,"datePublished":"2017-06-29T16:49:52.000Z","url":"https://community.spiceworks.com/t/disk-cleanup-across-domain-computers/590620/8","author":{"@type":"Person","name":"dhubbard","url":"https://community.spiceworks.com/u/dhubbard"}},{"@type":"Answer","text":"
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.<\/p>\n
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)<\/p>","upvoteCount":1,"datePublished":"2017-06-29T17:01:31.000Z","url":"https://community.spiceworks.com/t/disk-cleanup-across-domain-computers/590620/9","author":{"@type":"Person","name":"CrashFF","url":"https://community.spiceworks.com/u/CrashFF"}},{"@type":"Answer","text":"
this is like all<\/p>\n
#Windows disk cleanup\n\n& C:\\Windows\\System32\\cleanmgr.exe /sagerun:1\n\n#Temp + Recycle\n$Env:temp | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue\n\"$env:windir\\Temp\\*\" | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue\n$Recycler = (New-Object -ComObject Shell.Application).NameSpace(0xa)\n$Recycler.items() | foreach { rm $_.path -force -recurse }\n\n# Browsers\n\n #firefox cleanup\n\n Remove-Item -path \"$env\\users\\$env:username\\AppData\\Local\\Mozilla\\Firefox\\Profiles\\*.default\\cache\\*\" -Recurse -Force -EA SilentlyContinue -Verbose\n Remove-Item -path \"$env\\users\\$env:username\\AppData\\Local\\Mozilla\\Firefox\\Profiles\\*.default\\cache\\*.*\" -Recurse -Force -EA SilentlyContinue -Verbose\n Remove-Item -path \"$env\\users\\$env:username\\AppData\\Local\\Mozilla\\Firefox\\Profiles\\*.default\\cache2\\entries\\*.*\" -Recurse -Force -EA SilentlyContinue -Verbose\n Remove-Item -path \"$env\\users\\$env:username\\AppData\\Local\\Mozilla\\Firefox\\Profiles\\*.default\\thumbnails\\*\" -Recurse -Force -EA SilentlyContinue -Verbose\n Remove-Item -path \"$env\\users\\$env:username\\AppData\\Local\\Mozilla\\Firefox\\Profiles\\*.default\\cookies.sqlite\\*\" -Recurse -Force -EA SilentlyContinue -Verbose\n Remove-Item -path \"$env\\users\\$env:username\\AppData\\Local\\Mozilla\\Firefox\\Profiles\\*.default\\webappsstore.sqlite\\*\" -Recurse -Force -EA SilentlyContinue -Verbose\n Remove-Item -path \"$env\\users\\$env:username\\AppData\\Local\\Mozilla\\Firefox\\Profiles\\*.default\\chromeappsstore.sqlite\\*\" -Recurse -Force -EA SilentlyContinue -Verbose\n\n #chrome\n\n Remove-Item -path \"$env\\users\\$env:username\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Cache\\*\" -Recurse -Force -EA SilentlyContinue -Verbose\n Remove-Item -path \"$env\\users\\$env:username\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Cache2\\entries\\*\" -Recurse -Force -EA SilentlyContinue -Verbose\n Remove-Item -path \"$env\\users\\$env:username\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Cookies\\*\" -Recurse -Force -EA SilentlyContinue -Verbose\n Remove-Item -path \"$env\\users\\$env:username\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Media Cache\\*\" -Recurse -Force -EA SilentlyContinue -Verbose\n Remove-Item -path \"$env\\users\\$env:username\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Cookies-Journal\\*\" -Recurse -Force -EA SilentlyContinue -Verbose\n Remove-Item -path \"$env\\users\\$env:username\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\ChromeDWriteFontCache\\*\" -Recurse -Force -EA SilentlyContinue -Verbose\n\n # Internet Explorer\n\n\"$env\\users\\$env:username\\Local\\Microsoft\\Windows\\Temporary Internet Files\\Low\\*\" | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue\n\"$env\\users\\$env:username\\AppData\\Roaming\\Microsoft\\Windows\\Cookies\\Low\\*\" | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue\n\"$env\\users\\$env:username\\AppData\\Local\\Microsoft\\Windows\\History\\Low\\*\" | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue\n\"$env\\users\\$env:username\\AppData\\Local\\Temp\\Low\\*\" | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue\n\n#Delete logs\n\nRemove-Item -path \"C:\\windows\\logs\\CBS\\\" -recurse -force\n<\/code><\/pre>\n\n```\nCcleaner\n$test=Test-Path \"$env:programfiles\\CCleaner\"\nif($test -eq \"True\"){\n$os=(gwmi win32_operatingsystem).osarchitecture\nIf($os -eq \"64-bit\"){\n& \"c:\\Program Files\\CCleaner\\CCleaner64.exe\" \"/AUTO\"\n}\n\nelse\n{\n& \"c:\\Program Files\\CCleaner\\CCleaner.exe\" \"/AUTO\"\n}\n}\n \n```\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2017-06-29T17:17:27.000Z","url":"https://community.spiceworks.com/t/disk-cleanup-across-domain-computers/590620/10","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"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?<\/p>","upvoteCount":0,"datePublished":"2017-06-29T17:58:48.000Z","url":"https://community.spiceworks.com/t/disk-cleanup-across-domain-computers/590620/11","author":{"@type":"Person","name":"itnmok","url":"https://community.spiceworks.com/u/itnmok"}},{"@type":"Answer","text":"
Here’s an exact export of my functional GPO, including CleanMgr settings, and the Task Scheduler entries.<\/p>\n
Permission needs to be Authenticated Users, OU needs to be both Users and Computers for some reason. I forgot why.<\/p>\n