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


    • 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 ...
    • How to retrieve and analyze Microsoft 365 usage data with PowerShell

      Retrieving and analyzing Microsoft 365 usage data with PowerShell can provide valuable insights into how your organization is using Microsoft 365 services, which can help you optimize your environment and improve user productivity. In this ...
    • 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 ...
    • How to create and manage Microsoft 365 groups with PowerShell

      Introduction: Microsoft 365 Groups is a collaboration feature that allows users to work together and share resources such as calendars, files, and email messages. Microsoft 365 Groups can be created and managed using the Microsoft 365 admin center, ...