Last active
September 26, 2024 15:14
-
-
Save mortenya/f81e3217d3544d517076 to your computer and use it in GitHub Desktop.
Script to run as a Scheduled Task to clean out IIS logs older than 30 days.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Shamelessly liberated from http://foxdeploy.com/2015/02/11/automatically-delete-old-iis-logs-w-powershell/ | |
Because it was better than my own. | |
#> | |
$LogPath = "C:\inetpub\logs" | |
$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.FullName | 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment