Files
Azure/AutomationAccount/Invoke-OldFilePurge

Invoke-OldFilePurge

Overview

This PowerShell script deletes files older than a specified number of days from a target directory.

It is designed for high-volume deletion of small files — for example Tomcat temp folders, IIS logs, or other directories that accumulate large numbers of files over time. The function:

  • Scans a directory (optionally recursively) for files older than a given age
  • Deletes matching files using [System.IO.File]::Delete() for speed
  • Reports progress (percentage, rate, deleted/failed counts) via Write-Progress
  • Logs every deletion (and failure) to a timestamped log file
  • Supports -WhatIf / -Confirm via SupportsShouldProcess

Prerequisites

PowerShell

PowerShell 5.1 or later.


Files

Invoke-OldFilePurge.ps1

Defines the Invoke-OldFilePurge function. Dot-source the file or copy the function into your session/module before use.

. .\Invoke-OldFilePurge.ps1

Parameters

Parameter Mandatory Default Description
Path Yes Target directory to clean up.
DaysOld No 7 Files with LastWriteTime older than this many days are deleted. Range: 13650.
Recurse No Enabled If specified, subdirectories are also scanned.
LogDir No C:\Temp Directory for the log file. Created automatically if it does not exist.
LogEnabled No $true Enables or disables logging.

Running the script

Preview what would be deleted without actually deleting anything:

Invoke-OldFilePurge.ps1 -Path "C:\Program Files\Apache Software Foundation\Tomcat 9.0\temp" -WhatIf

Delete files older than 7 days (default):

Invoke-OldFilePurge.ps1 -Path "C:\Program Files\Apache Software Foundation\Tomcat 9.0\temp"

Delete files older than 30 days and write logs to a custom directory:

Invoke-OldFilePurge.ps1 -Path "D:\IIS\Logs" -DaysOld 30 -LogDir "D:\Logs"

Run without logging:

Invoke-OldFilePurge.ps1 -Path "D:\Temp" -LogEnabled $false

Logging

Each execution (when LogEnabled is $true) creates a log file:

FileCleanup_YYYYMMDD_HHMMSS.log

Example:

2026-07-02 09:12:11  [INFO ]  === Invoke-OldFilePurge started ===
2026-07-02 09:12:11  [INFO ]  Path      : D:\IIS\Logs
2026-07-02 09:12:11  [INFO ]  DaysOld   : 30 (cutoff: 2026-06-02 09:12:11)
2026-07-02 09:12:11  [INFO ]  Recurse   : True
2026-07-02 09:12:11  [INFO ]  Files to delete: 1245
2026-07-02 09:12:12  [INFO ]  DELETED: D:\IIS\Logs\u_ex260101.log
2026-07-02 09:12:12  [WARN ]  FAILED : D:\IIS\Logs\u_ex260102.log — file in use
2026-07-02 09:13:05  [INFO ]  Completed in 00:00:53.42 — Deleted: 1244 | Failed: 1 | Total: 1245
2026-07-02 09:13:05  [INFO ]  === Invoke-OldFilePurge finished ===

Progress reporting

While running, the script displays a progress bar updated every 500 processed files (or on the last file), showing the count deleted, failed, and the deletion rate (files/second).


Error Handling

  • Validates that Path exists and is a directory before starting.
  • Each file deletion is wrapped in try/catch; failures are counted and logged with [WARN] but do not stop the run.
  • Supports -WhatIf and -Confirm (via SupportsShouldProcess) to preview or confirm deletions before they happen.

Notes

  • Files are matched purely on LastWriteTime; there is no filter on file extension or name.
  • Hidden and system files are included (-Force).
  • Deletion uses [System.IO.File]::Delete() instead of Remove-Item, which is significantly faster for large numbers of small files.