In this tutorial, we cover how to quickly and easily cleanup all the "junk" that accumulates on PCs by automating in PowerShell.
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:
Let's build a script that will invoke these actions as well as act as a framework to build other tasks into.
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.
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. 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:
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, 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.
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!
Adam Bertram is a 25+ year IT veteran and an experienced online business professional. He’s a successful blogger, consultant, 6x Microsoft MVP, trainer, published author and freelance writer for dozens of publications. For how-to tech tutorials, catch up with Adam at adamtheautomator.com, connect on LinkedIn or follow him on X at @adbertram.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites