This script deletes all Exchange and IIS logs older than X days from all Exchange 2013+ servers that are fetched using the Get-ExchangeServer cmdlet.
The Exchange log file location is read from the environment variable and used to build an administrative UNC path for file deletions.
Optionally, you can use the Active Directory configuration partition to determine the Exchange install path dynamically, if supported in your Active Directory environment.
- It is assumed that the Exchange setup path is IDENTICAL across all Exchange servers, when not using the UseDynamicExchangePaths parameter
- The IIS log file location is read from the local IIS Metabase of the LOCAL server where the script is executed and is used to build an administrative UNC path for IIS log file deletions
- It is assumed that the IIS log file location is identical across all Exchange servers
- Use IsEdge switch when running on an Exchange Server holding the EDGE role
# EXAMPLE 1# Delete Exchange and IIS log files older than 14 days .\Purge-LogFiles -DaysToKeep 14# EXAMPLE 3# Delete Exchange and IIS log files older than 7 days with automatic discovery.\Purge-LogFiles -DaysToKeep 7 -Auto# EXAMPLE 3# Delete Exchange and IIS log files older than 7 days with automatic discovery and send email report.\Purge-LogFiles -DaysToKeep 7 -Auto -SendMail -MailFrom firstname.lastname@example.org -MailTo email@example.com -MailServer mail.sedna-inc.com# EXAMPLE 4# Delete Exchange and IIS log files older than 14 days, but copy files to a central repository and compress the log files before final deletion.\Purge-LogFiles -DaysToKeep 14 -RepositoryRootPath \\OTHERSERVER\OtherShare\LOGS -ArchiveMode CopyZipAndDelete# EXAMPLE 5# Delete Exchange Server, IIS, and HTTPERR log files older than 7 days, and send an HTML email. Identify Exchange file paths using AD configuration objects..\Purge-LogFiles.ps1 -DaysToKeep 7 -SendMail -MailFrom firstname.lastname@example.org -MailTo email@example.com -MailServer mail.sedna-inc.com -UseDynamicExchangePaths -IncludeHttpErr
- 2.3 Option for HTTPERR added, Option for dynamic Exchange install paths added, Html formatting added, tested with Exchange Server 2019
- 2.21 Issue #12 fixed
- 2.2 Minor fixes, but no changes
- 2.14 Issue #9 fixed | IsEdge parameter added
- 2.13 Issue #7 fixed | SendMail switch query
- 2.11 Issue #6 fixed | Missing Remove-Item cmdlet
- 2.1 Logfile archiving and archive compressions added
- 2.0 CopyFilesBeforeDelete implemented
- 1.93 Minor chances to PowerShell hygiene
- 1.92 Count issue fixed to run on Windows Server 2012
- 1.91 Write DaysToKeep to log
- 1.9 Global functions updated (write to event log)
- 1.8 Support for global logging and other functions added
- 1.6 Count Error fixed 1.7 Email report functionality added
- 1.5 Sorting of server names added and Write-Host output changed
- 1.4 Handling of IIS default location fixed
- 1.3 Check if running in elevated mode added
- 1.2 Auto/Manual configuration options added
- 1.1 Variable fix and optional code added
- 1.0 Initial community release
- This PowerShell script requires the GlobalFunctions module, described here.
When using PowerShell 5 or later you can simply use Install-Module GlobalFunctions to install the module to your system.
- Windows Server 2012 R2 or newer
- ActiveDirectory PowerShell module
- Exchange 2013+ Management Shell
- Download and follow at Github: https://github.com/Apoc70/Purge-LogFiles
- Download and like at TechNet Gallery: https://gallery.technet.microsoft.com/Purge-Exchange-Server-2013-c2e03e72
Brian Reid, C7 Solutions, http://www.c7solutions.com/2013/04/removing-old-exchange-2013-log-files-html