Clean up IIS log files from web server using PowerShell

Avoid low disk space because of IIS logs with PowerShell Scheduled Task

Microsoft IIS Server is by default logging every request that reaches it.

This can be pretty useful for tracking down the issue which my occur over time, but it is also pilling up log files on server's disk which eventually can cause low disk space. You an always clean up these files manually, but wouldn't it be nice if server does that for you?

You can always write your custom code and place it as a service on web server, but remember, it is Windows, it already has a lot of out of the box tools.

So easiest way it to use Powershell. You can always use batch but Powershell gives you greater flexibility and it is more powerful at trust me, a lot easier to write.

The following Powershell script will loop through websites and it's configured log folders and delete files which are older that one week.

Import-Module WebAdministration

#Maximum age in days of files to be deleted
$logfileMaxAge = 7
foreach($website in $(Get-Website))
	#Get log folder for current website
	#Get all log files in the folder
	$files = Get-ChildItem $folder -Filter *.log

	foreach($file in $files){
		if($file.LastWriteTime -lt (Get-Date).AddDays(-1*$logfileMaxAge)){
			#Remove fie older than logfileMaxAge days
			Remove-Item $file.FullName


Now when we have cleanup code, we just need to schedule it. For that we can use Windows TaskScheduler. It can be found at Control Panel -> Administrative Tools -> Task Scheduler

You can do it manually, but lets say you need to do it on more than one machine, it might become a headache.

I had the same issue, so I wrote a Powershell which will setup ScheduledTask which executes another Powershell script. Use previous script and save it to any place on the disk. I placed it at D:\PowershellScripts\IISLogCleanup.ps1 which I also used in the next sample as a parameter for scheduled task:

$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument 'D:\PowershellScripts\IISLogCleanup.ps1'
$trigger = New-ScheduledTaskTrigger -Daily -At 3am
$prncipal = New-ScheduledTaskPrincipal -GroupId "BUILTIN\Administrators" -RunLevel Highest
Register-ScheduledTask -Action $action -Trigger $trigger -Principal $prncipal -TaskName "IIS Log Cleanup" -Description "Daily clean up of IIS logs"

You need to run this script under Administrator privileges in order to create the task with the policy defined in this script. It is necessary in order for script to loop through IIS registered websites

Now when you have your scheduled task setup, you can stop worrying that your disk space will be overtaken by IIS log files :) 



Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.

About the author


Dejan is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform including ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus


read more


read more


read more

Umbraco CMS

read more

Comments for this article