How To Delete Files Older Than x Days with PowerShell

How To Delete Files Older Than x Days with PowerShell

In today’s fast-paced digital world, managing the clutter of old files and maintaining an organized system is essential for smooth operations and optimal performance. However, manual file cleanup can be time-consuming and prone to errors.

That’s where PowerShell, a powerful scripting language in Windows, comes to the rescue! With its robust capabilities, PowerShell offers a simple yet effective solution to delete files older than a specified number of days, automating the tedious task and freeing up valuable storage space.


Delete files that are older than certain days.

After one of my favorite tools of the last years (delage32) was warned by the manipulation protection of some security applications, I recreated the function with Microsoft Windows PowerShell. The result is a small script that quickly deletes all files with age exceeding a freely definable age.

Moreover, there is a restriction to specific file extensions. This makes sense if other files in the checked directory may not be deleted.

  How to Pause PowerShell Script

Brief description:

  • $Source: This variable stores the source folder containing the files to be deleted.
  • $Days: Number of Days after which files are deleted
  • $ext: Array where the file extensions to be deleted are stored.

The script deletes recursively, including all files in any subfolders. If this is not desired, the “-recurse” parameter must be removed in the penultimate line. No (sub)folders are deleted, only files. All deleted files are written to a log file stored in the specified source folder.


Delete Files older than x day (PowerShell script)

# Description: 
# This script deletes files that are a certain number of days old. The file extensions, the age, as well as the storage location are definable. 
# The deletion contains all subfolders. All operations will be written to a log file, stored in the source folder. 
# !!! use at your own risk !!! 
# Here you can define the source folder, the age of the files (in days) and the file extensions 
$Source = "C:Temp" # Important: Ends with "" 
$Days = 90 # Number of days from which files are deleted 
$ext = ".txt",".log" # Array - add more extensions with ,".xyz" 
$log = "$Source$(get-date -format yymmddHHmmss).txt" 
$DateBeforeXDays = (Get-Date).AddDays(-$Days) 

# Start Script 
$log write-host "--------------------------------------------------------------------------------------" 
write-host "Deletetion of all files ($ext) in folder $Source which are older than $Days days." 
write-host "--------------------------------------------------------------------------------------" 
get-childitem $Source* -include $ext -recurse | where {$_.lastwritetime -lt $DateBeforeXDays -and -not $_.psiscontainer} | remove-item -force -verbose

The script output looks like this:

  How to Delete Empty Folders Using Powershell

You can also download the complete script as a file here (zip, 9 kb). You can ideally use the script for cleaning up folder structures by running it through Windows Task Scheduler periodically.

Important: Please Use the script at your own risk. I assume no liability for any damage.

Rohit is a certified Microsoft Windows expert with a passion for simplifying technology. With years of hands-on experience and a knack for problem-solving, He is dedicated to helping individuals and businesses make the most of their Windows systems. Whether it's troubleshooting, optimization, or sharing expert insights,