Skip to content

Instantly share code, notes, and snippets.

@ByronScottJones
Last active April 6, 2023 15:12
Show Gist options
  • Save ByronScottJones/5733f931c6b75bde72f77975c08efee1 to your computer and use it in GitHub Desktop.
Save ByronScottJones/5733f931c6b75bde72f77975c08efee1 to your computer and use it in GitHub Desktop.
#Save this file to C:\Scripts\clean-iislogs.ps1
#Run this command to create the schedule task:
# schtasks /create /sc DAILY /tn "Clean IIS Logs" /tr "powershell.exe -ExecutionPolicy Bypass C:\Scripts\clean-iislogs.ps1" /rl highest /ru system /st "03:00"
$FilePath = "C:\inetpub\logs"
$FileExt = "*.log"
$maxDaystoKeep = 90
$DeleteDateTime = ((get-date).AddDays(-$maxDaystoKeep))
#Get a list of files to be deleted
$itemsToDelete = Get-ChildItem $FilePath -File $FileExt -Recurse | Where-Object LastWriteTime -lt $DeleteDateTime
if ($itemsToDelete.Count -gt 0)
{
ForEach ($item in $itemsToDelete)
{
Get-item $item.FullName | Remove-Item -Verbose
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment