Delete Files Older Than x Days with PowerShell

How to delete files older than x days? And, important, without additional software. You find the solution in the Windows PowerShell. In this article I will describe how it works and what you have to pay attention for.



Delete files which 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 have recreated the function with the Microsoft Windows PowerShell. The result is a small script that quickly deletes all files whose age has exceeded a freely definable age.

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

Short description:

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

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

Delete Files older than x day (PowerShell Skript)

# Author: René Albarus - https://www.tech-faq.net 
# Date: 2019/04/16 
# 
# 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 
start-transcript 
$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
stop-transcript

The script output looks like this:

Delete Files Older Than x Days
Files, that were older than two days, have been deleted by the script.

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

Important: The application of the script happens at your own risk. I assume no liability for any damage.

Schreibe einen Kommentar