Jump to content

How to Cleanup IIS Log Files

Recommended Posts

Try this:

$Path = "C:\inetpub\logs\LogFiles"
$Daysback = "-120"

$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

You can adjust the file path to the log files and the amount of days in the top two sections.


If you want to add it to an Automate script to schedule, then use:
Function: Execute Script
Script Type: PowerShell
Script to Exexute: <paste above script with adjustments if needed>
Script Parameters: <leave blank>
Script Credentials: Run as Local Agent
Variable: powershellresults

The powershell variable is pretty arbitrary because it's just going to return 'OK', but you're welcome to make the script a little more verbose to return better results.

Edited by bigdog09
  • Thanks 1

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...