0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Clean Temp Files Quickly

Use Get-ChildItem -Path $env:TEMP -Recurse | Remove-Item -Force -ErrorAction SilentlyContinue in PowerShell to clean temp files recursively and quietly.
📋

Examples

Input$env:TEMP contains 3 files: file1.tmp, file2.tmp, file3.tmp
OutputAll 3 files are deleted without error messages.
Input$env:TEMP contains nested folders with temp files
OutputAll files in all folders under $env:TEMP are deleted recursively.
Input$env:TEMP is empty
OutputNo files found; script runs without errors or output.
🧠

How to Think About It

To clean temp files, identify the temp folder path using the environment variable $env:TEMP. Then, list all files and folders inside it recursively. Finally, delete these files and folders safely, ignoring errors like locked files.
📐

Algorithm

1
Get the path of the temp folder from environment variables.
2
List all files and folders inside this temp folder recursively.
3
Delete each file and folder found, forcing deletion and ignoring errors.
4
Print a confirmation message after cleanup.
💻

Code

powershell
Write-Output "Cleaning temp files in $env:TEMP"
Get-ChildItem -Path $env:TEMP -Recurse -Force | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
Write-Output "Temp files cleaned successfully."
Output
Cleaning temp files in C:\Users\User\AppData\Local\Temp Temp files cleaned successfully.
🔍

Dry Run

Let's trace cleaning temp files in a folder with 2 files and 1 subfolder containing 1 file.

1

Identify temp folder

Temp folder path is C:\Users\User\AppData\Local\Temp

2

List files recursively

Found: file1.tmp, file2.tmp, subfolder\file3.tmp

3

Delete files and folders

Deleted file1.tmp, file2.tmp, file3.tmp, then subfolder

ItemAction
file1.tmpDeleted
file2.tmpDeleted
subfolder\file3.tmpDeleted
subfolderDeleted
💡

Why This Works

Step 1: Get temp folder path

Using $env:TEMP gets the current user's temp folder path dynamically.

Step 2: List all files and folders

The Get-ChildItem -Recurse command lists all files and folders inside the temp folder and its subfolders.

Step 3: Delete items safely

The Remove-Item -Force -Recurse -ErrorAction SilentlyContinue deletes all items, forcing deletion and ignoring errors like locked files.

🔄

Alternative Approaches

Delete only files, keep folders
powershell
Write-Output "Cleaning only temp files in $env:TEMP"
Get-ChildItem -Path $env:TEMP -Recurse -File | Remove-Item -Force -ErrorAction SilentlyContinue
Write-Output "Temp files cleaned, folders kept."
This method deletes only files, preserving folder structure, useful if you want to keep empty folders.
Use Clear-Content for large log files
powershell
Write-Output "Clearing content of large temp log files"
Get-ChildItem -Path $env:TEMP -Recurse -Filter *.log | ForEach-Object { Clear-Content $_.FullName }
Write-Output "Log files cleared."
Instead of deleting, this clears content of large log files to save space without removing files.

Complexity: O(n) time, O(1) space

Time Complexity

The script visits each file and folder once, so time grows linearly with the number of items in the temp folder.

Space Complexity

The script uses constant extra space as it processes items one by one without storing them all simultaneously.

Which Approach is Fastest?

Deleting only files is slightly faster than deleting files and folders recursively because it skips folder removal steps.

ApproachTimeSpaceBest For
Delete files and folders recursivelyO(n)O(1)Complete cleanup including folders
Delete only filesO(n)O(1)Keep folder structure intact
Clear content of log filesO(m)O(1)Free space without deleting files
💡
Run PowerShell as administrator to avoid permission issues when deleting temp files.
⚠️
Forgetting to use -Recurse causes only top-level temp files to be deleted, leaving nested files untouched.