How to delete all temp files using PowerShell

Delete all temp folders contents

The unfortunate side effect of using Windows as a client operating system is that temporary folders can accumulate over time and take up considerable space in the database or hard drives and end up slowing the computer considerably. However, deleting the contents of all the temporary files manually isn't scalable. This is where PowerShell scripts can be of help. Execute the following script to delete all the temporary file contents in a go.

Script

<#
.SYNOPSIS
This script can be used to delete all temp files.
.DESCRIPTION
This script can be used to delete all temp files.
.EXAMPLE
C:\PS> C:\Script\Temp_Files_Delete.ps1
Deletes all temp files in the system.
#>
#Fetches all temp folders and deletes files in it recursively.

$tempfolders = @(“C:\Windows\Temp\*”, “C:\Windows\Prefetch\*”, “C:\Documents and Settings\*\Local Settings\temp\*”, “C:\Users\*\Appdata\Local\Temp\*”)
Remove-Item $tempfolders -force -recurse