Execute PowerShell script: example of common requirement to delete log files older than a specified interval

Bob Bradley 7 years ago • updated by anonymous 3 years ago 2

I used the following script to delete rolling log files generated by both a SSIS package as well as the FIM Operations History files ... thereby making sure disk space usage was kept within limits. This would be a useful script to post on the Event Broker wiki as an example usage of the "Execute PowerShell Script" plug-in ... just copy this code:

# Delete all *.log Files in target folder older than 21 day(s)
$Path = "\\MyServerFileShare\Logs\MyApplication"
$Daysback = "-21"
$Extension = "*.log"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Include $Extension -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item