---
title: "Temp File Cleaner: A PowerShell Script Guide"
description: "Reclaim your storage space. Learn to build a custom temp file cleaner with PowerShell."
canonical: "https://adamtheautomator.com/temp-file-cleaner/"
---

# Temp File Cleaner: A PowerShell Script Guide

> Reclaim your storage space. Learn to build a custom temp file cleaner with PowerShell.

Source: https://adamtheautomator.com/temp-file-cleaner/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Temp File Cleaner: A PowerShell Script Guide](https://adamtheautomator.com/wp-content/uploads/2019/07/5d238ae5c824b514689ea638.jpg)

# Temp File Cleaner: A PowerShell Script Guide

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)1 July 20192 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[File Management](/tag/file-management/)[PowerShell](/tag/powershell/)

Table of Contents

*   [Removing User Temp Files](#h-removing-user-temp-files)
*   [Automating Windows Disk Cleanup](#h-automating-windows-disk-cleanup)
*   [Wrap Up](#h-wrap-up)

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](https://adamtheautomator.com/get-childitem/)_**

```powershell
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`](https://adamtheautomator.com/powershell-to-delete-files/ "Remove-Item").

```powershell
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.

```powershell
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.

![Temp File Cleaner : Windows disk cleanup UI](https://adamtheautomator.com/wp-content/uploads/2020/12/image-4.png)

Temp File Cleaner : Windows disk cleanup UI

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](https://github.com/adbertram/Random-PowerShell-Work/blob/master/File-Folder%20Management/Invoke-WindowsDiskCleanup.ps1). 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](https://mcpmag.com/articles/2017/02/02/exploring-dot-sourcing-in-powershell.aspx) 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!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Ftemp-file-cleaner%2F&text=Temp%20File%20Cleaner%3A%20A%20PowerShell%20Script%20Guide)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Ftemp-file-cleaner%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Ftemp-file-cleaner%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2021/08/Icacls-The-Ultimate-Guide.jpg)

### [Icacls: The Ultimate Guide in Managing File Permissions](/icacls/)

Learn how to grant, deny, remove permissions, and more with the icacls command in this ultimate guide!

![](https://adamtheautomator.com/wp-content/uploads/2019/07/5d238ae5c824b514689ea65f.jpg)

### [Automate File Transfers with PowerShell & Scheduled Tasks](/automated-file-transfer/)

Set up automated file transfers using PowerShell and create scheduled tasks to run your script on a recurring basis.

![](https://adamtheautomator.com/wp-content/uploads/2019/07/file-cannot-accessed-fix-handle-exe-denied.png)

### [Unlock File Locks: Sysinternals Handle & PowerShell](/sysinternals-handle/)

Discover what process has a file locked. Learn to use Sysinternals Handle with PowerShell.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
