Often times we need to rotate log files to prevent them from growing too large.  This simple task can become difficult when the log files are in use by an application.  The scripts below help overcome this obstacle with the use of PowerShell.


The scripts below copy the specified log file to a new name and then truncate the current file.  Both RotateLogsTruncate.ps1 & RotateLocalLogsTruncate.ps1 perform the same function.  The difference is that RotateLogsTruncate.ps1 will enumerate Active Directory for a list of servers whereas RotateLocalLogsTruncate.ps1 works on a single, local server.

How to Use

The following scripts take the parameters below:

  • $TargetFolder
    This is the path to the log files WITHOUT the server name.  e.g. c$\inetpub\logs\PHPLogs\
  • $LogFile
    This is the log file you want to rotate.  e.g. php_errors.log or *.log
  • $SearchRoot (not used by RotateLocalLogsTruncate.ps1)
    The Active Directory path to obtain a list of servers from.  e.g. ou=Servers,dc=contoso,dc=local
  • $Recurse
    Optional.  Should the script recurse the directory specified by $TargetFolder
  • $MailTo
    E-mail address to send the report to.
  • $MailFrom
    E-mail address to use as the from address
  • $MailServer
    IP or DNS name of your mail server


Code – RotateLogsTruncate.ps1


Code – RotateLocalLogsTruncate.ps1


Have suggestion or comments?  Drop me an email.  These scripts are available on my BitBucket repo as well.

Leave a Reply

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