First head over to the Microsoft Script Gallery and grab deleteold.ps1 by Jaap Brasser.
I tend to copy scripts to c:\scripts on systems so I copied deleteold.ps1 there and created two new batch files there as well.
The first was used to launch the powershell script with switches that will delete all files in the IIS Logs directory older than 90 days and append to a log of all actions taken.
IISLogsCleanUp.bat :
1 |
powershell.exe -ExecutionPolicy bypass -File C:\scripts\deleteold.ps1 -FolderPath C:\inetpub\logs -FileAge 90 -VerboseLog -AppendLog -LogFile "C:\scripts\logcleanup.log" |
The second batch file registers a task that runs this script weekly. Right click this batch and run as administrator and IIS logs will no longer be a thorn in your side.
IISLogCleanUpTaskReg.bat :
1 |
schtasks /create /tn "CleanUpIISLogs" /ru "SYSTEM" /tr "c:\scripts\IISLogsCleanUp.bat" /sc weekly /mo 1 /d SUN /st 05:01 |
As usual, customize as needed! Next on my list is for this repetitive task is creating a powershell remoting script to copy these files to remote servers and execute the task register command.