Simple scheduled log file clean up with PowerShell 1.0

Servers accumulate gigabytes of log files over time. Anyone who has managed Microsoft’s IIS Service will know it collects data until your hard disk is full. You can set a log rollover based on interval or file size but IIS does not do any housekeeping per se.

I support a number of ageing Windows SBS 2011 and Windows Server 2008 machines. After nearly a decade of updates all of them have disk space issues. Log files need careful management. Obviously, check you’re not legally obliged to keep this data.

Suppose you have an IIS installation with a default log file location of C:\inetpub\logs\LogFiles\ . Each site will have logs located in folders such as C:\inetpub\logs\LogFiles\W3SVC<websiteID> .

This PoweShell script will delete log files inside the W3SVC<websiteID>  folder after 5 days:

set-location C:\inetpub\logs\LogFiles\W3SVC1\

foreach ($File in get-childitem) {
   if ($File.LastWriteTime -lt (Get-Date).AddDays(-5)) {
      del $File
   }
}

Put the script in a suitably named file and folder and set-up a daily scheduled task.

Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Arguments: “C:\IISLogCleaner\deletelogs.ps1”

PowerShell Log File Clean Up Scheduled Actions
PowerShell Log File Clean Up Scheduled Actions

 

You May Also Like