Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

PowerShell Log Cleanup

A configurable log cleanup script for Windows servers. Removes old log files based on retention period, file types, and target directories. Supports dry-run mode, JSON config files, and per-directory recursion control.

Built to run as a scheduled task to prevent log directories from filling up disks.

Quick Start

# Delete log files older than 14 days
.\Invoke-LogCleanup.ps1 -Paths "C:\inetpub\logs","C:\app\logs" -RetentionDays 14

# Dry run - see what would be deleted without removing anything
.\Invoke-LogCleanup.ps1 -Paths "C:\inetpub\logs" -RetentionDays 30 -WhatIf

# Use a JSON config file
.\Invoke-LogCleanup.ps1 -ConfigPath ".\cleanup-config.json"

Parameters

Parameter Description Default
-Paths Directories to clean. Append :norecurse to disable recursive scan for a path -
-RetentionDays Files older than this many days are deleted 30
-FileTypes File extensions to target *.log, *.txt, *.csv, *.etl
-ConfigPath Path to a JSON config file (overrides other params) -
-WhatIf Preview mode - no files are deleted -

Config File Format

{
    "RetentionDays": 14,
    "FileTypes": ["*.log", "*.txt", "*.csv"],
    "Paths": [
        { "Path": "C:\\inetpub\\logs\\LogFiles", "Recurse": true },
        { "Path": "C:\\app\\logs", "Recurse": false }
    ]
}

See cleanup-config.example.json for a starter template.

Output

=== Log Cleanup ===
Retention   : 14 days (files before 2026-04-02)
File types  : *.log, *.txt, *.csv, *.etl
Directories : 2

Scanning: C:\inetpub\logs\LogFiles (recursive)
  Deleted 142 files (89.3 MB)
Scanning: C:\app\logs (top-level only)
  Deleted 17 files (2.1 MB)

--- Summary ---
Deleted: 159 files (91.4 MB)

Scheduled Task Setup

# Run daily at 2 AM
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -File C:\scripts\Invoke-LogCleanup.ps1 -ConfigPath C:\scripts\cleanup-config.json"
$trigger = New-ScheduledTaskTrigger -Daily -At "2:00AM"
Register-ScheduledTask -TaskName "Log Cleanup" -Action $action -Trigger $trigger -RunLevel Highest -User "SYSTEM"

Use Cases

  • IIS log rotation - clean up W3C log files on web servers
  • Application log maintenance - keep app log directories under control
  • Exchange log cleanup - remove old transport and diagnostic logs
  • General disk hygiene - schedule across any server fleet via Group Policy

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages