Clean Up Those SharePoint Logs!

To help keep my SharePoint log files under control I have created a simple PowerShell script that automatically deletes files from a directory and all subdirectories that are over a set number of days old.   Once the log files are cleaned up the script will then delete any left over empty subdirectories.

You can download this script and use it as is or modify it for your specific needs.   I would recommend modifying the $Days and the $TargetFolder variables in the script to match your specific needs and environment.   The $Days indicates how many days of logs should be kept.  I have this set at 5.  I figure that if my SharePoint environment starts having issues I will most likely only care about the most recent days events.   The $TargetFolder should be set to the SharePoint log folder.  

Note:  Although I have tested this on several environments I will not be responsible for any damages or loss that you may experience.  In other words:  Use at your own risk.    To run the script you will need to set the execution policy to unrestricted by typing Set-ExecutionPolicy unrestricted at your PowerShell command prompt.  Before doing this please take a moment to review the effects of this command and understand any additional security risks it may create.

 

Leave a Reply