IIS Log File cleaner

All too often I run across web servers with log files being stored until the drive runs out of space. If you are running IIS 7 or later, the following powershell script can automate this purging process.

#==============================================
#       --- Configuration Options ---
#Provide the name of the site
$myweb = "Default Web Site"
#Provide the number of days you wish to keep
$days = 90
#whatif toggle, verify only option
$whatif = $true
#==============================================

Import-Module WebAdministration

$myweb = Get-WebSite -Name "$myweb"
$mylogdir = $myweb.logFile.directory

if($whatif)
{
get-childitem $mylogdir -recurse | 
	where {$_.lastwritetime -lt (get-date).adddays(-$days) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force -WhatIf }
	}
else
{
get-childitem $mylogdir -recurse | 
	where {$_.lastwritetime -lt (get-date).adddays(-$days) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force -ErrorAction SilentlyContinue} | Write-Host $_.fullname
}

Leave a comment

Your email address will not be published. Required fields are marked *