Tagged: IIS

Remove old logfiles

Want to clean out old logfiles from IIS (or other products)?

PARAM (
    [int] $daysBack = 7,
    [string] $logPath = "C:\Inetpub\Logs\LogFiles"
)

Get-ChildItem $logPath -Recurse -Include *.LOG | Where-Object {$_.CreationTime -lt (Get-Date).AddDays(0-$daysBack)} | ForEach-Object {
	Write-Host "Processing: " -ForegroundColor Yellow -NoNewline
	Write-Host $_.FullName -ForegroundColor White -NoNewline
	
	$span = New-TimeSpan $_.CreationTime $(get-date)
	Write-Host " $($span.Days) days old" -ForegroundColor Yellow -NoNewline

	TRY {
        Remove-Item $_.FullName -Force -ErrorAction Stop
        Write-Host " [Deleted]" -ForegroundColor Green
    }

    CATCH {
        Write-Host " [Can't delete]" -ForegroundColor Red
    }
}