How Can We Help?

Search for answers or browse our knowledge base.

< All Topics

Script for Cleaning Up Windows IIS Log Files

Over time, Windows IIS log files can accumulate and consume valuable disk space. Regularly cleaning up these log files is essential to maintain system performance and manage storage efficiently. This knowledge base article provides a script to help you automate the process of cleaning up IIS log files on your Windows server.

Step-by-Step Guide:

1. Create a PowerShell Script:

  • Open a text editor (e.g., Notepad) and create a new file.
  • Copy and paste the following PowerShell script into the file:

2. Save and Run the Script:

  • Save the file with a .ps1 extension (e.g., CleanupIISLogs.ps1).
  • Open PowerShell as an administrator.
  • Navigate to the directory where you saved the script using the cd command.
  • Run the script by entering its name (e.g., .\CleanupIISLogs.ps1) and pressing Enter.

# Set the path to the IIS log directory
$logPath = “C:\inetpub\logs\LogFiles\”

# Set the number of days to retain log files
$daysToKeep = 30

# Get the current date
$currentDate = Get-Date

# Calculate the date to determine which log files to delete
$dateToCompare = $currentDate.AddDays(-$daysToKeep)

# Get a list of log files older than the specified number of days
$oldLogFiles = Get-ChildItem -Path $logPath | Where-Object { $_.LastWriteTime -lt $dateToCompare }

# Delete the old log files
$oldLogFiles | ForEach-Object {
Remove-Item $_.FullName -Force
Write-Host “Deleted file: $($_.FullName)”

Write-Host “Log file cleanup completed.”

3. Schedule the Script (Optional):

  • To automate the cleanup process, you can schedule the script to run at specific intervals using the Windows Task Scheduler.
  • Open the Task Scheduler and create a new task.
  • Configure the task to run the PowerShell script at your desired frequency (e.g., daily or weekly).

Additional Notes:

  • Ensure that you have the necessary permissions to delete files in the IIS log directory.
  • Adjust the $logPath variable to match the actual path of your IIS log files.
  • Modify the $daysToKeep variable to set the number of days you want to retain log files.


Automating the cleanup of Windows IIS log files using the provided PowerShell script can help you maintain a clean and organized server environment. By regularly removing outdated log files, you can free up disk space and ensure the optimal performance of your IIS server.

Was this article helpful?
0 out of 5 stars
5 Stars 0%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 0%
Please Share Your Feedback
How Can We Improve This Article?
Table of Contents