Skip to content

Instantly share code, notes, and snippets.

@mortenya
Last active March 7, 2024 17:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mortenya/f81e3217d3544d517076 to your computer and use it in GitHub Desktop.
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.
<#
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