PowerShell scirpt to delete old IIS log files

powershell iis log file deletion 6 years, 3 months ago
$LogPath = "c:\inetpub\logs\LogFiles\W3SVC2\" $maxDaystoKeep = -30 $outputPath = "c:\cleanuptask\Cleanup_Old_logs.log" $itemsToDelete = dir $LogPath -Recurse -File *.log | Where LastWriteTime -lt ((get-date).AddDays($maxDaystoKeep)) if ($itemsToDelete.Count -gt 0){ ForEach ($item in $itemsToDelete){ "$($item.BaseName) is older than $((get-date).AddDays($maxDaystoKeep)) and will be deleted" | Add-Content $outputPath Get-item $item | Remove-Item -Verbose } } ELSE{ "No items to be deleted today $($(Get-Date).DateTime)" | Add-Content $outputPath } Write-Output "Cleanup of log files older than $((get-date).AddDays($maxDaystoKeep)) completed..." start-sleep -Seconds 10
1134
Posted By