Let’s start with that log files retention should be handle by backup mechanisms. But sometimes backup is handle by different layer (f.e. Virtualization) and You still want to manage your logs.
There are few suggestions how to handle it and one of those is via PowerShell Script. Today I wanted to provide You with my variation of Log Purge script and how to set it up in Task Scheduler.
You can download the scrip over here LogPurge4.ps1
Script base on 2 parameters .
First is Integer that provide number of days between 0 and 365 that sets log files maximum age.
Second is path to text file that contains list of folders with log files. (Example to download – this example have default path to IIS and Exchange log files, remember to check if yours are not redirected)
So if we want to call script from PowerShell that will Purge f.e. IIS logs older than 30 days we need to provide text file that will have just one line (if You store IIS log files in default path o.c.)
And call it from PowerShell
.\LogPurge4.ps1 30 Example.txt
This will check all the subfolders from C:\inetpub\logs and search for files with extension *.log or *.blg that are older than 30 days and delete it. It will also generate report file with list of all processed folders and files and name it LogPurge-FileList.txt
This is quite neet. Now if u want to schedule it in TaskSheduler, You have to remember few things.
- Call Powershell just in case and provide script as parameter.
- And if u provide full path to Folders list file or script use quote ” so string will be interpreted as one parameter
- Always good to provide start in folder.
Here is Example of Action in task.
LogPurge4.ps1 and TargetFoldrer.txt are in C:\Scripts\Log\
Action: Start Program
Arguments: C:\Scripts\Log\LogPurge4.ps1 30 TargetFolder.txt
Star in: C:\Scripts\Log\