Partager via


Disk Cleanup in a MDT Task Sequence

While using MDT to remaster an existing Windows 7 image I added tasks to apply Windows 7 Service Pack 1 and a slew of other updates to the image (so that deployed workstations are up to date out of the gate instead of waiting for updates to come down to each system across the network).  Unfortunately this bloated the end product WIM by almost 2 GB.  I started to look into ways I could cleanup any unnecessary files on the hard drive before capturing the image.  Instead of just hacking away at the disk, deleting temporary files and other caches, I decided to try to integrate the built-in Disk Cleanup utility.

Disk Cleanup, cleanmgr.exe, has command line parameters that are well documented but there is little to nothing on how to automate this, especially in the context of a MDT task sequence.  The typical process is to run cleanmgr.exe /sageset:n (where n is an arbitrary integer) and set the files to delete in the interface.  This then configures a cleanup profile in the registry so that when running cleanmgr.exe /sagerun:n it has a preset configuration to use.

To automate this for the task sequence I made the decision that every cache type (aka handler) is to be deleted in this scenario, including the one most important to me: Service Pack Backup Files.  I wrote the attached MDT-style script that loops through each subkey under HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches and sets the StateFlags0042 value to DWORD 2 (in this example, n = 42).  There is an article on MSDN that includes an excellent description of the StateFlags value.  The script then runs cleanmgr.exe /sagerun:42 to do the needful.

I added this cleanup task to the end of the Custom Tasks group of the State Restore phase (for those of you using the Standard Client Task Sequence).  It cleaned almost 3 GB of data from the system, which resulted in the final WIM only growing by 575 MB from the starting custom image (instead of almost 2 GB without the cleanup).

Disclaimer: The information on this site is provided "AS IS" with no warranties, confers no rights, and is not supported by the authors or Microsoft Corporation. Use of included script samples are subject to the terms specified in the Terms of Use .

CustomDiskCleanup.zip

Comments

  • Anonymous
    January 01, 2003
    Excellent, thank you.
  • Anonymous
    July 09, 2014
    This seems to hang and run forever has anyone else experienced this issue?
  • Anonymous
    December 14, 2015
    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.

    I'm wondering if this is what 'Ben' is referring to.
  • Anonymous
    January 26, 2016
    The Cleanmgr.exe-utility is not at all well documented, as stated by the Microsoft employee. His script hangs because the utility is only silent for SOME of the cleaning operations (does the documentation say that? No it does not). This is true for the previous windows installation folder ("Windows.old"), and obviously true for some other cleaning actions as well. The utility is therefore useless for theese specific cleaning actions. I must say, this is Microsoft in a nutshell.