Product and service reviews are conducted independently by our editorial team, but we sometimes make money when you click on links. Learn more.

How to Automate the Windows Disk Cleanup Utility

By - Source: Toms IT Pro

Credit: rvlsoft/ShutterstockCredit: rvlsoft/Shutterstock

The more use a computer gets, the slower it tends to become, and the more storage is consumed. Over time, we install software, download programs; those programs then extract temporary files who knows where, we throw files into that folder we think we'll get back to, but never do, and so on. Over time, all of this activity can culminate into a computer that has low disk space and is noticeably slower than it was when new.

One way to revitalize a computer is to either reimage it or just wipe the drive completely and install Windows from scratch. I like to call this the nuclear approach. It definitely does the trick but is time-consuming and requires countless hours getting all of the programs and settings configured exactly like they were pre-nuke. If you'd rather perform a cleanup without the nuclear approach, you can empty the recycle bin, clean out temporary files and so on yourself but you'll ultimately forget a step here and there and plus this is what a script is for.

MORE: Windows 10 for IT Pros: Tutorials, Tips and Tricks

Depending on the configuration and software installed, each computer may require different activities to be performed. To take care of the least common denominator, we're going to automating the Windows Disk Cleanup utility. This utility takes care of a lot of work identifying and cleaning up various areas. The Windows Disk Cleanup utility has a GUI that can run but we're automating this so you can download a community script by running Install-Script -Name Invoke-WindowsDiskCleanup.

By default, the script enables all of the categories possible with the Windows Disk Cleanup utility. Those categories are: Active Setup Temp Folders,Branch Cache,Content Indexer Cleaner,Device Driver Packages,Downloaded Program Files,Game News Files,Game Statistics Files,Game Update Files,Internet Cache Files,Memory Dump Files,Offline Pages Files,Old ChkDsk Files,Previous Installations,Recycle Bin,Service Pack Cleanup,Setup Log Files,System error memory dump files,System error minidump files,Temporary Files,Temporary Setup Files,Temporary Sync Files,Thumbnail Cache,Update Cleanup,Upgrade Discarded Files,User file versions,Windows Defender,Windows Error Reporting Archive Files,Windows Error Reporting Queue Files,Windows Error Reporting System Archive Files,Windows Error Reporting System Queue Files,Windows ESD installation files,Windows Upgrade Log Files.

That's a lot of categories. But, the script also has a Section parameter that allows the user to only perform one or more categories rather than the entire set.

To run the Windows Disk Cleanup script with all of these categories enabled, simply run .Invoke-WindowsDiskCleanup.ps1 with no parameters. Below, I'm just using the Verbose parameter to get some detail as to what it's doing:

PS> .Invoke-WindowsDiskCleanup.ps1 -Verbose
VERBOSE: Clearing CleanMgr.exe automation settings.
VERBOSE: Adding enabled disk cleanup sections...
VERBOSE: Starting CleanMgr.exe...
VERBOSE: Waiting for CleanMgr and DismHost processes..

If you'd like to only enable a few of the categories, you can also use the Section parameter to specify one or more of the individual categories.

PS> .Invoke-WindowsDiskCleanup.ps1 -Section 'Memory Dump Files','Offline Pages Files','Old ChkDsk Files'

This script creates a PowerShell "building block" as well. If you're an IT administrator, chances are you're not just going to have one computer to clean up buts lots. By using PowerShell remoting and a little extra code, we can copy this script to remote computers and run it remotely as well!

$localScriptPath = 'C:Program FilesWindowsPowerShellScriptsInvoke-WindowsDiskCleanup.ps1'
$computers = 'PC1','PC2'

foreach ($comp in $computers) {
    try {
        $remoteFile = Copy-Item -Path $localScriptPath -Destination "\\$comp\c$" -PassThru
        Invoke-Command -ComputerName $comp -ScriptBlock { C:Invoke-WindowsDiskCleanup.ps1 }
    } catch {
        Write-Error $_.Exception.Message
    } finally {
        if (Get-Variable -Name remoteFile -ErrorAction Ignore) {
            #Remove-Item -Path $remoteFile.FullName -ErrorAction