Automating the Disk Cleanup Utility

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.

Download DiskCleanup_81.ps1


#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

}

Download DiskCleanup_81.ps1

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

 

About Greg Ramsey
Greg Ramsey is a systems engineer specializing in global systems management for Dell IT. He has a B.S. in Computer Sciences and Engineering from the Ohio State University and is a Microsoft Most Valuable Professional (MVP) for Microsoft System Center Configuration Manager. Greg coauthored SMS 2003 Recipes: A Problem-Solution Approach (Apress, 2006), Microsoft System Center Configuration Manager Unleashed (Sams, 2009), Microsoft System Center 2012 Configuration Manager Unleashed (Sams, 2012), and Microsoft System Center 2012 Configuration Manager: Administration Cookbook (Packt Publishing, 2012), and Microsoft System Center 2012 Configuration Manager R2 Unleashed (Sams, 2012). Greg is cofounder of the Ohio SMS Users Group, and the Central Texas Systems Management User Group.

9 Responses to Automating the Disk Cleanup Utility

  1. George Simos says:

    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!

  2. JuliusPIV says:

    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?

  3. Cor says:

    diskcleanup for windows 7 or windows 10?

  4. Cor says:

    Is it possible to write perform cleanup after the deploy ? instead during sysprep

  5. Surya says:

    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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: