How to Manage Log Files Retention with PowerShell

Every application offers some sort of logging functionality and different ways to configure it.  It’s a cornerstone of the software life-cycle from the moment the app is developed, tested, deployed, even more on a daily production usage and support.  One characteristic of the logging process is for how much time do I need to keep my log files, in three words the retention period.

But if the application doesn’t allow to configure the retention period is better to prevent that a big quantity of log files will fill all ours disk space.

In this simple oneliner we will delete all files in the current folder (but you can customise the path and file type if needed)  that are older than 30 days.

Instead of delete-item you can create an archive by using 7zip or Compress archive.

TIP: Enabling the compression on the log folder can reduce logs to about 2% of the original size.

Once we save the script we can schedule it to run in task scheduler

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.