Powershell Script. Remove old IIS Logs.

I don’t like to keep IIS logs for too long. Just over a week is plenty in my environment. To keep these under control , I run a powershell script with an automated task once a week.

There are likely better ways to manage this or better ways to write the script but in saying that, it works for me.

$Servers = “server1” , “server2”
$dir = “\driveLetter$\inetpub”
$daysOldLimit = 8
Set-Location MyScriptFolderLocation

$servers | foreach {
$server = $_

Get-ChildItem -Path (“\\” + $_ + $dir) -filter *.log -Recurse | foreach {

$timestamp = [datetime]$_.lastwritetime
$daysOld = (NEW-TIMESPAN –Start ([datetime]$_.lastwritetime) –End (get-date) | select TotalDays).TotalDays

If ($daysold -gt $daysOldLimit)
$FullName = $_.FullName
Remove-Item -Path $FullName




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s