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


    Limited-period offers on our IAM solutions




      • Related Articles

      • Clear Contents of a File

        Temporary files can take up considerable space in a database and eventually slow down computers. Administrators can use the following PowerShell script to completely clear the contents of files remotely. If you want to delete the entire text file ...
      • Powershell Script to Delete Files Older than X days

        Files and 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 them manually isn't quite scalable. This is where PowerShell scripts can be of ...
      • Create a shared folder

        Provisioning and managing shared folders through the native Windows process is a pretty straightforward process. However, things can get quickly out of hand when you need to manage multiple shares across multiple computers. With PowerShell, you can ...
      • WMI privileges for a non-administrator user using Powershell

        Administrators often find themselves working against time. In an environment where the time is of essence, certain day-to-day tasks like password resets, enabling remote access, etc., can be rationed off of technicians. However, technicians are ...
      • How to manage inactive Active Directory user accounts

        Over time, an organization's Active Directory (AD) network can start accumulating inactive user accounts. These accounts can be of employees who may have left the organization, temporary accounts, etc. The problem here is that these inactive AD ...