Click here to Skip to main content
11,790,613 members (62,102 online)
Click here to Skip to main content

Tagged as

Simple Powershell script to clean up IIS log files

, 3 Oct 2013 CPOL 12.5K 2
Rate this:
Please Sign up or sign in to vote.
Very simple PowerShell cleanup script that will clean IIS 7 log files


Most administrators come across cleanup tasks once in a while or when disk space is full. As we know most of the disk space gets occupied by log files and that could be any log files on windows or third party application running on windows.

In this article I will be showing very simple PowerShell cleanup script that will clean IIS 7 log files. One can use this sample and modify it to fit to other log files cleanup tasks. 

I have separated cleanup functionality into a simple function called CleanTempLogfiles() which is very generic. It takes simple file path($FilePath) as parameter and removes all files older than specified days provided in the variable ($days).In following sample script it removes all files older than 7 days. 

Powershell code 

# Module: Powershell script to clean IIS log files 
Set-Executionpolicy RemoteSigned
(Get-Variable Path).Options="ReadOnly"
Write-Host "Removing IIS-logs keeping last" $days "days"

function CleanTempLogfiles()
param ($FilePath)
    Set-Location $FilePath
    Foreach ($File in Get-ChildItem -Path $FilePath)
        if (!$File.PSIsContainerCopy) 
            if ($File.LastWriteTime -lt ($(Get-Date).Adddays($days))) 

            remove-item -path $File -force
            Write-Host "Removed logfile: "  $File


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chandra Hundigam
United States United States
Chandra Hundigam has Master degree in Computer Application, Microsoft Certified Professional and Software Architect. He's significantly involved in enterprise application development and distributed object oriented system development using Microsoft .Net, Sun Java/J2EE technology to serve global giants in the Media, Finance, Mortgage and Software Industries.Presently working as Independent Software Consultant for a US-based company.His areas of interests are in emerging Technologies.

You may also be interested in...

Comments and Discussions

QuestionAutomatic detection of log files Pin
sai.akhil198910-May-15 22:33
membersai.akhil198910-May-15 22:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1509028.1 | Last Updated 4 Oct 2013
Article Copyright 2013 by Chandra Hundigam
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid