Skip to content

Instantly share code, notes, and snippets.

@devtdq1701
Forked from mortenya/Remove-OldIISLogs.ps1
Created July 15, 2023 11:25
Show Gist options
  • Save devtdq1701/2b8985754a2f9a0baa6a94b528b5b39f to your computer and use it in GitHub Desktop.
Save devtdq1701/2b8985754a2f9a0baa6a94b528b5b39f 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