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.

DQo8cD48YnIvPg0KPHNjcmlwdCBhc3luYyBzcmM9Ii8vcGFnZWFkMi5nb29nbGVzeW5kaWNhdGlvbi5jb20vcGFnZWFkL2pzL2Fkc2J5Z29vZ2xlLmpzIj48L3NjcmlwdD4NCjwhLS0gVEVDSEZBUSAtIEltIEFydGlrZWwgLS0+DQo8aW5zIGNsYXNzPSJhZHNieWdvb2dsZSINCiAgICAgc3R5bGU9ImRpc3BsYXk6YmxvY2siDQogICAgIGRhdGEtYWQtY2xpZW50PSJjYS1wdWItOTI5NDg1MzEyMDc1NDI3OSINCiAgICAgZGF0YS1hZC1zbG90PSI1MDc3NjQ3MTY5Ig0KICAgICBkYXRhLWFkLWZvcm1hdD0iYXV0byI+PC9pbnM+DQo8c2NyaXB0Pg0KKGFkc2J5Z29vZ2xlID0gd2luZG93LmFkc2J5Z29vZ2xlIHx8IFtdKS5wdXNoKHt9KTsNCjwvc2NyaXB0Pjxici8+PC9wPg0K

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.

Pin It on Pinterest