In this tutorial, we cover how to quickly and easily clean up all the “junk” that accumulates on PCs by automating in PowerShell. Build your own temp file cleaner!
One of the unfortunate side effects of using Windows as a client operating system is the eventual accumulation of “junk.” Depending on how much the PC is used, what’s installed and how often programs are added and removed, a PC can accumulate a ton of unnecessary junk.
This unusable material takes up storage and may potentially affect the PC’s performance. What’s labeled as “junk” depends heavily on the user and the context the PC is used in, but there are some areas of Windows that can be safely cleaned up without much fuss.
We can usually safely perform a few actions:
- Remove user temporary folder contents
- Remove user temporary Internet files
- Run the Disk Cleanup utility
Let’s build a script that will invoke these actions as well as act as a framework to build other tasks into.
Removing User Temp Files
Before building the temp file cleaner, we’ll start with removing user temporary folder contents. To perform this step, we’ll first need to figure out where all of these folders are. Once we’ve figured out where the folders are, we then need to enumerate each of the files within and remove them.
Because I know that each user’s temp folder is in their profile and all profiles are stored in C:\Users\%UserName%\AppData\Local\Temp, I can enumerate all files in these folders using Get-ChildItem
.
Related: Get-ChidItem: Listing Files, Registry Items, Certificates as One
Get-ChildItem -Path 'C:\Users' | foreach {
Get-ChildItem -Path "$($_.FullName)\AppData\Local\Temp" -ErrorAction Ignore
}
To remove all of the files in each of these temp folders, we can pipe the files directly to Remove-Item
.
Get-ChildItem -Path 'C:\Users' | foreach {
Get-ChildItem -Path "$($_.FullName)\AppData\Local\Temp" -ErrorAction Ignore | Remove-Item -Force -Recurse
}
Since we’re already looping through each user profile, we can quickly add the step to remove each user’s temporary Internet files as well.
Get-ChildItem -Path 'C:\Users' | foreach {
Get-ChildItem -Path "$($_.FullName)\AppData\Local\Temp" -ErrorAction Ignore | Remove-Item -Force -Recurse
Get-ChildItem -Path "$($_.FullName)\AppData\Local\Temporary Internet Files" -ErrorAction Ignore | Remove-Item -Force -Recurse
}
At this point, we’ve cleared up each user’s temp files as well as their temporary Internet files. Your temp file cleaner is done!
Automating Windows Disk Cleanup
Let’s now use a built-in Windows utility called Disk Cleanup to give us a shortcut on cleaning up a ton of other everyday things.
Because automating the Disk Cleanup utility isn’t as cut and dry as clearing out user temp and temp internet files, I’ve created a function called Invoke-WindowsDiskCleanup
downloadable from my Github repository. This function, when invoked, can enable any or all of the Disk Cleanup rules and remove anything that the utility normally would. These rules include:
- Active Setup Temp Folders
- BranchCache
- Content Indexer Cleaner
- Device Driver Packages
- Downloaded Program Files
- GameNewsFiles
- GameStatisticsFiles
- GameUpdateFiles
- 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
To remove unnecessary rules, open up the Invoke-WindowsDiskCleanup
function, and remove the specific rules in the enabledSections
variable. To run this function either dot-source it or copy it directly into your cleanup script. Once available, it can be invoked via calling Invoke-WindowsDiskCleanup
. This will enable every rule inside of the enabledSections
variable, execute the Disk Cleanup utility, and wait for it to complete.
Wrap Up
By the time each of these tasks has been incorporated into a cleanup script, you’re well on your way! At this point, figure out what, if any, company-specific tasks you’d also like to incorporate and add to the script. Once a cleanup script has been created, it can then be updated to perform any other cleanup task necessary!