Deleting specific log files by extension older than X days using PowerShell

Over the weekend, a colleague of mine approached me and asked about a method to delete log files over a specific age with a specific file extension on Windows Server. This sounded like an excellent task for PowerShell and the Windows Task Scheduler.

A quick Google search yielded many options. The sticking point for this use case was the fact that the log folder contained various logs (different file names and extensions) for various purposes. This is a common situation with Apache Tomcat. Most of the scripts and methods I discovered only took date (older than X days) into account but not the name or extension of the file.

I liked Thomas Maurer’s post on this topic and ended up adapting his script as follows:

REPLACE:

Get-ChildItem $Path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

WITH:

Get-ChildItem $Path | Where-Object { $_.Name -like '*.ext' } | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

This adds a second “filter” (Where-Object), based on the filename. The file extension is part of the Name attribute - in the example above, I used .ext.

The completed script looked like this:

$Path = "C:\temp"
$Daysback = "-60"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path | Where-Object { $_.Name -like '*.ext' } | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

Once created, I scheduled in Windows Task Scheduler as follows:

Screenshot of Task Scheduler Action

This worked well for the use case at hand, but would likely not scale for multiple extensions or file names.