Application logs can stop your machine if you do not control their size. By default, webserver IIS doesn’t clean IIS logs and stores them in c:\inetpub\logs. If your server is high loaded, logs of IIS can get critical size in 2-3 weeks.
In this script I remove all the IIS log files older than 10 day. It’s value is hardcoded, but you can change it.
#Get log files from all subdirectories
$iislogs = Get-ChildItem C:\inetpub\logs\LogFiles –Recurse
#select only files which are older than 10 days
$iislogsrem = $iislogs | where {$_.LastWriteTime -le (Get-Date).AddDays(-10) -and !$_.PSIsContainer}
#removing files
$iislogsrem | Remove-Item
Or you can combine it to one expression:
$iislogs = Get-ChildItem C:\inetpub\logs\LogFiles –Recurse | where {$_.LastWriteTime -le (Get-Date).AddDays(-10) -and !$_.PSIsContainer} | Remove-Item
Of course, you can easily modify this script to remove any logs or other files.
Leave a Reply