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 adminstrative UNC path for file deletions.
# 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 email@example.com -MailTo firstname.lastname@example.org -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
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.
Brian Reid, C7 Solutions, http://www.c7solutions.com/2013/04/removing-old-exchange-2013-log-files-html