ClearTempFiles: The Ultimate Guide to Freeing Up Disk Space

Written by

in

Automating temporary file cleanup on Windows can be achieved efficiently using either PowerShell or a Batch script. Automated cleanup helps optimize system performance, reclaims disk space, and eliminates corrupted cache data without requiring manual intervention.

The scripts target three core areas: the current user’s temporary folder (%temp%), the system-wide temporary folder (C:\Windows\Temp), and the Windows Prefetch directory (C:\Windows\Prefetch) which caches application components. Option 1: The PowerShell Approach (Recommended)

PowerShell offers robust error handling and allows you to target files based on their age, preventing the accidental deletion of temporary files currently in active use. The Script

Create a new file using a text editor, paste the following code, and save it as ClearTempFiles.ps1: powershell

# Define target temporary directories \(TempFolders = @( "\)env:TEMP*”, “C:\Windows\Temp*”, “C:\Windows\Prefetch*” ) # Loop through each folder path and forcefully remove items inside foreach (\(Path in \)TempFolders) { Get-ChildItem -Path \(Path -Recurse -ErrorAction SilentlyContinue | Where-Object { \)_.LastWriteTime -lt (Get-Date).AddDays(-1) } | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue } Use code with caution. Why it works:

Simple way to remove or delete temporary files in Windows 10

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *