Back in Nov 2006 I wrote a blog post on creating a batch command file that could be used to cleanup old IIS log files. Then, in Dec 2007, I wrote an updated blog post with another solution. I thought I would update those posts for 2013 using PowerShell.
OLD SCHOOL (Batch command file)
Both of the above solutions were written in the SBS 2003 era, but will work with SBS 2008/2011. Both versions will let you identify the number of days to keep (I suggest 30 days), and will scan sub-folders under the specified folder.
As a quick review, here’s how to implement my Dec 2007 solution:
- Download the zip file from my web site which contains three files: .vbs, .bat, and .log files.

- Extract and copy those three files to your desired directory. I normally use c:\scripts.
- Edit the .bat file to point to the appropriate parent folder of the log files to be cleaned up, and the number of days of log files to retain.
For Windows 2008 R2, my .bat file looks like this:
- NOTE: If running this for the first time, you may wish to test drive the script, without actually deleting any files. To do this, edit the deloldfiles.vbs file and comment out the ‘file.delete’ line in the script by adding a single quote (‘) to the start of the line. If all works, go back and remove the single quote.
- After testing the script manually, all that is left is to schedule the script to run on a weekly or monthly basis, as desired.
NEW SCHOOL (PowerShell)
While batch files are still supported in the Windows 2008 R2/Windows 2012 era, we need to start getting comfortable with using PowerShell scripts. I found a handy PowerShell script that does this from the Microsoft forum.
This script used the “start-transcript / stop-transcript” command to create a history log of files deleted.
- Download the PowerShell script file (deloldfiles.ps1) from my web site.
- Edit the script, as necessary, to point to the appropriate parent folder and the number of days to retain, and save.
- NOTE: the script includes a “–whatif” option that allows us to run the script without actually deleting anything. If the script appears to work as expected, then simply remove the –whatif option from the script. Be sure to leave the trailing brace } in the script.

- To run, right click on the .ps1 file and click ‘Run with PowerShell’
- After testing the script manually, don’t forget to schedule it to run on a weekly or mothly basis.
Finally, from the FWIW department: you should know that from a security point of view,
deleting IIS log files or other similar system log files, is not generally recommended.

