Delete Files Older Than x Days

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)

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

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.