Automating the Disk Cleanup Utility
May 14, 2014 10 Comments
After using a system for several months, installing multiple hotfixes, and even upgrading the operating system, your system will have some excess file bloat. You can manually run the disk cleanup utility to cleanup the excess files, or you can automate the cleanup process.
Here’s a sample PowerShell script to automate the disk cleanup utility in Windows 8.1. I have purposely written this so that it only runs on Windows 8.1, as different features may be able to be cleaned up on different operating system versions.
#more info here: #http://support.microsoft.com/kb/253597 #ensure we're running on windows 8.1 first if ((Get-CimInstance win32_operatingsystem).version -eq '6.3.9600') { #Capture current free disk space on Drive C $FreespaceBefore = (Get-WmiObject win32_logicaldisk -filter "DeviceID='C:'" | select Freespace).FreeSpace/1GB #Set StateFlags0012 setting for each item in Windows 8.1 disk cleanup utility if (-not (get-itemproperty -path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders' -name StateFlags0012 -ErrorAction SilentlyContinue)) { set-itemproperty -path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\BranchCache' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Memory Dump Files' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Old ChkDsk Files' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Service Pack Cleanup' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Setup Log Files' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error memory dump files' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error minidump files' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Thumbnail Cache' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Update Cleanup' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Upgrade Discarded Files' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\User file versions' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Defender' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Archive Files' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Queue Files' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Archive Files' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Queue Files' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows ESD installation files' -name StateFlags0012 -type DWORD -Value 2 set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Upgrade Log Files' -name StateFlags0012 -type DWORD -Value 2 } cleanmgr /sagerun:12 do { "waiting for cleanmgr to complete. . ." start-sleep 5 } while ((get-wmiobject win32_process | where-object {$_.processname -eq 'cleanmgr.exe'} | measure).count) $FreespaceAfter = (Get-WmiObject win32_logicaldisk -filter "DeviceID='C:'" | select Freespace).FreeSpace/1GB "Free Space Before: {0}" -f $FreespaceBefore "Free Space After: {0}" -f $FreespaceAfter }In this script, we’re basically setting all the registry settings as if you had run cleanmgr.exe /sageset:12 to configure the settings. And then we run cleanmgr.exe /sagerun:12 to instruct cleanmgr to use the configured settings to run unattended.
To create settings for a different OS or version, follow the steps in http://support.microsoft.com/kb/253597 to manually run cleanmgr.exe with the /sageset:12 switch, to set the information in the registry properly. Then navigate to HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\ key and extract the information and update the script below.
When you run the script (as an administrator), you will see the “Disk Cleanup” utility run – sometimes I’ve seen two windows appear at the same time, cleaning two items at the same time. The script make take several minutes to run, depending on the amount of content to be purged.
Happy Scripting!
Greg
This post first appeared on http://www.gregramsey.net
Great article Greg!
Please allow me to note that in Windows 8 and later there is already an out of the box scheduled task that does the component store cleanup every 30 days (I’m talking about the hotfixes cleanup).
You can verify this by going to Task Scheduler > Task Scheduler Library > Microsoft > Servicing and the task is named ‘StartComponentCleanup’.
Also there is a Disk Cleanup task in Task Scheduler > Task Scheduler Library > Microsoft > Windows > DiskCleanup and it is named ‘SilentCleanup’, the command line it executes is “%windir%\system32\cleanmgr.exe /autoclean /d %systemdrive%”.
I’m not saying that your solution becomes redundant because you are selecting more things to clean than those two already in the O/S tasks, however I hope you add these two clarifications in your article to give a better perspective of how Windows 8.1 handles the system maintenance better than previous versions.
Regards!
Excellent points George! I agree – StartComponentClenaup handles a bit of things automatically. We did see an additional .5-2GB of freed disk space after running the automated disk cleanup utility, so I agree, there’s some space for each solution. I think I’ll have to do a little research and testing before I can give more information than what is found here: http://technet.microsoft.com/en-us/library/dn251565.aspx – let me know if you beat me to it!
Greg
Glad you like the points Greg!
I think you will like more the following blog posts from Microsoft and Redmond Magazine that describe MS’ commitment to provide a better auto-clean of the component store with less reboots for Windows 8.1 (at least).
Here are the posts:
This is the 3-Part blog post from the Core Team about the component store servicing
1) http://blogs.technet.com/b/askcore/archive/2014/03/06/what-s-new-in-windows-servicing-part-1.aspx
2) http://blogs.technet.com/b/askcore/archive/2014/03/27/what-s-new-in-windows-servicing-reduction-of-windows-footprint-part-2.aspx
3) http://blogs.technet.com/b/askcore/archive/2014/04/21/what-s-new-in-windows-servicing-service-stack-improvements-part-3.aspx
And from Redmond Magazine that gotten me to the above ones:
1) http://redmondmag.com/articles/2014/03/07/windows-8-winsxs-cleanup.aspx
I hope you find them valuable as I did 🙂
If I get some free time I’ll investigate more about optimizing the cleanup process and will update you.
George
I’m finding that in a Build & Capture of Windows 7, the cleanmgr consistently hangs on the ‘Windows Upgrade Log Files’ step. Everything prior to that (Windows ESD Installation FIles … Upgrade Discarded Files … Service Pack Cleanup etc.) works fine; it only ‘hangs’ on the ‘Windows Upgrade Log Files’ step.
Is anyone else seeing this?
diskcleanup for windows 7 or windows 10?
This example is for 8.1. I believe it’S a similar process for 7-haven’t looked at this for 10 yet. Let us know how your teats go!
Is it possible to write perform cleanup after the deploy ? instead during sysprep
What deployment method are you using?
If you use MDT or/and SCCM you may add a task step before finishing the Deployment Task Sequence.
Great Article,
we have twenty windows 2008 servers in a network, need to cleanup remotely.
I like schedule task on one server and execute on remote servers like
Example:
DiskCleanup.ps1 –ComputerName parameter
Please can you help, I am new to power shell.
Thanks.
lol great script lol