Removing Old IIS Logs

Simple powershell script to remove old IIS logs (or any other file for that matter).

$pathToLogs = “C:\WINDOWS\system32\LogFiles\W3SVC1”;
$LogAgeTarget = 183; # Number of days of logs to keep
$dteNow = Get-Date

    if (Test-Path $pathToLogs){
      $LastWrite = $Now.AddDays(-$LogAgeTarget)
      $Files = Get-ChildItem $pathToLogs -include *.log -recurse |
            Where {$_.LastWriteTime -le “$LastWrite”}

      foreach ($File in $Files){
        Remove-Item $File -WhatIf | Out-Null
        }

    } Else {
        # Log Path does not exist
        Write-Host = “Log Path does not exist.”
    }

This can be set up as a scheduled task in the control panel using the following as the command to run:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe “& ‘c:\scripts\pruneIISlogs.ps1′”

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>