Over the last few weeks I have upgraded and reinstalled a number of Windows 10 machines and as part of my routine after an upgrade to a later build of Windows 10 I would clean up both the Upgrade files as well as the old version of Windows. The steps to do this involves starting the Disk Cleanup tool, with Administrative credentials, and clicking through the interface in order to clean up the files and save up some of those precious GBs of storage space. After repeating this process a few times in the past week it started to become a bit tedious.
In order to do this is use the command line options that are available with the Disk Cleanup tool, cleanmgr.exe. While it is unfortunately not possible to use this tool directly to clean up specifically the left over files of an Upgrade of Windows 10, it does allow for using the GUI to create a ‘StateFlags’ which is stored in the registry. Using this methodology in combination with Sysinternals Procmon I established where this information was written in the registry and I came up with the following three lines of PowerShell code that allowed me to create the job required to do this job:
1 2 3
New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files' -PropertyType 'DWORD' -Force -Name 'StateFlags1337' -Value 0x2 New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations' -PropertyType 'DWORD' -Force -Name 'StateFlags1337' -Value 0x2 cleanmgr.exe /SAGERUN:1337
This will code will first create two registry keys and after creating those registry keys, specify the Disk Cleanup application to run with job number 1337. This worked, although unfortunately it is not possible to hide the GUI completely as user interaction might still be required. In certain scenarios you will be prompted to confirm that you really do want to delete the Old Windows Installation files, that includes the warning that deleting those files will not allow you to roll back to an older version of Windows anymore. This also includes the files if you upgrades from Windows 7/8.1 to Windows 10.
As I saw more potential for this method of utilizing the Disk Cleanup tool, I decided to write a proper module that does simplifies the job creation process beyond just cleaning up Previous Windows Installation / Upgrade files. Today I have released the module that does contains the following functions:
To install the module on your system you can run the following code:
Install-Module DiskCleanup -Verbose
To remove the Previous Windows Installation / Upgrade files run the following command:
Remove-WindowsUpgradeFiles -Verbose -Confirm:$false
This will both create the job with number 1337 and then execute that job, and the Disk Cleanup Windows will appear on your screen. This process can take a few minutes depending on the speed of your system. The function will wait until all Disk Cleanup windows have been closed and then show a report of the amount of space saved.
It is also possible to setup a custom job using the Set-VolumeCachesStateFlags cmdlet, the mandatory parameter -StateFlags will accept any number of Switch parameters. These parameters are dynamically generated based on the available Keys in the registry path:
HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files
The module is available at the following online repositories:
- PowerShell Gallery – DiskCleanup
- TechNet Script Gallery – DiskCleanup
- GitHub – JaapBrasser – DiskCleanup
I will actively maintain this module, so feel free to leave a comment or feature request in the comments or send in a pull request on GitHub.
All the links mentioned in this article are available below:
|Links in this Article|
|PowerShell Gallery – DiskCleanup|
|TechNet Script Gallery – DiskCleanup|
|GitHub – JaapBrasser – DiskCleanup|
|Sysinternals – Process Monitor|