Microsoft Windows – Automate Disk Cleanup

Release date: April 19th 2020

Welcome to my Microsoft Tips & Tricks section. Everyone knows that patching your Windows Servers are important. In my lab I have automated the downloading, installing and restarts of my Windows servers. But cleaning up old updates and logs etc. have been done manually up until now. I have used the Disk Cleanup utility periodically and manually cleaned up the disks. This is tiresome and boring. As this is a lab-environment, I want to automate as much as possible to be able to spend time labbing exiting setups instead of doing maintenance. Therefore I have made this little session describing how I did this.

The first thing to do is to define what the Disk Cleanup utility should clean up. I launch an administrative command prompt and run the following command…

cleanmgr.exe /sageset:1
ScheduleDiskCleanup-01

I check the items I want Disk Cleanup to automatically clean

ScheduleDiskCleanup-02
ScheduleDiskCleanup-03

This will populate the following registry key: HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches

Next I create a Scheduled task that runs Disk Cleanup weekly

ScheduleDiskCleanup-04
ScheduleDiskCleanup-05
ScheduleDiskCleanup-06
ScheduleDiskCleanup-07
ScheduleDiskCleanup-08
ScheduleDiskCleanup-09
ScheduleDiskCleanup-10

When I have my new task created, I do a test run of this task.

Now, if I only had one server that needed weekly disk cleanup, this would have been sufficient. Unfortunately this isn’t the case. To distribute this setting I will create a GPO which will deploy the registry key I created when running sageset above. Using the same GPO I also create a scheduled task as described above.

This will ensure that all my servers get the Scheduled Weekly Disk Cleanup Schedule with the correct registry settings. More time for me to drink coffee and focus on other things…

Microsoft Tips & Tricks section

Disclaimer: Every tips/tricks/posting I have published here, is tried and tested in different it-solutions. It is not guaranteed to work everywhere, but is meant as a tip for other users out there. Remember, Google is your friend and don’t be afraid to steal with pride! Feel free to comment below as needed.