Exchange servers can accumulate a lot of IIS log files over time. Some administrators configure IIS to store logs on a different disk to avoid problems, while others just wait for free disk space alerts and manually remove old logs from time to time.

I found a number of PowerShell scripts online for automating the cleanup of IIS log files, but none were an exact match for what I wanted. I had a few objectives for this script:

No external dependencies (many scripts rely on command line versions of zip utilities like 7-Zip)

Must compress log files into monthly archive zip files, not just delete them

Must have the ability to store the zip files in a central archive location

So I wrote IISLogsCleanup.ps1, which I am making available for download here.

Download the script from the TechNet Script Gallery or Github.

How to Run IISLogsCleanup.ps1

Please test the script on a non-production server first or at least make sure you have backed up your IIS log files before trying this script for the first time.

The script takes two parameters:

Logpath – this is a mandatory parameter to specify the path to the IIS logs you want to clean up, such as “D:IIS LogsW3SVC1”

– this is a mandatory parameter to specify the path to the IIS logs you want to clean up, such as “D:IIS LogsW3SVC1” ArchivePath – this is an optional parameter to specify the path to the central archive location, such as “\

as01\archives\iislogs”

When you run IISLogsCleanup.ps1 it performs the following:

Calculates the first day of the previous month (so there will always be at least 1 month of retained logs)

Zips up log files from before the first day of the previous month into zip files per month

Verifies the results of the zip action and removing the log files if safe to do so

Optionally, moves the zip file to the central archive location

Writes a log file of progress and actions taken

Examples:

.\IISLogsCleanup.ps1 -Logpath "D:\IIS Logs\W3SVC1" .\IISLogsCleanup.ps1 -Logpath "D:\IIS Logs\W3SVC1" -ArchivePath "\

as01\archives\iislogs" 1 2 3 . \ IISLogsCleanup . ps1 - Logpath "D:\IIS Logs\W3SVC1" . \ IISLogsCleanup . ps1 - Logpath "D:\IIS Logs\W3SVC1" - ArchivePath "\

as01\archives\iislogs"

Scheduling IISLogsCleanup.ps1

To run the script as a scheduled task use the following task settings (replace server names and file paths as necessary):

Run whether user is logged on or not

Triggers: I recommend the first day of each month

Action: Start a program Program: powershell.exe Arguments: -command “C:\Scripts\IISLogsCleanup.ps1 -LogPath ‘C:\inetpub\logs\LogFiles\W3SVC1’ -ArchivePath ‘\\ho-mgt\iislogbackups'”



To run the task with a least privilege service account the account needs:

Rights to “Log on as a batch job” for the local server

Read/write access to the IIS logs directory

Write access to the archive location

Read/write/execute to the location where the script is running from

Download the script from the TechNet Script Gallery or Github.

As always if you have any questions or feedback please leave a comment below.

Updated 8/8/2015 – thanks to Rob and Alain in the comments for the suggestions for regional date format issues and zip file locking issues.