Update to most current release if you are using v2.1
PowerShell script for modern Exchange Server environments to clean up Exchange Server and IIS log files.
This repository is archived. You can find the most recent release of this script here.
This script deletes all Exchange Server 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 a variable and used to build an administrative UNC path for file deletions. It is assumed that the Exchange setup path is IDENTICAL across all Exchange servers.
Optionally, you can use the Active Directory configuration partition to determine the Exchange install path dynamically, if supported in your Active Directory environment.
The IIS log file location is read from the local IIS metabase of the LOCAL server and is used to build an administrative UNC path for IIS log file deletions.
Currently, it is assumed that the IIS log file location is identical across all Exchange servers.
- Windows Server 2012 R2 or newer
- Utilizes the global function library found here: http://scripts.granikos.eu
- AciveDirectory PowerShell module
- Exchange Server 2013+
- Exchange Management Shell (EMS)
- 2020-05-14, v2.3.1, Issues #14, #15 fixed to properly support Edge Transport Role
- 2020-03-12, v2.3, Option for HTTPERR added, Option for dynamic Exchange install paths added, Html formatting added, tested with Exchange Server 2019
Number of days Exchange and IIS log files should be retained, default is 30 days
Switch to use automatic detection of the IIS and Exchange log folder paths
Indicates the the script is executed on an Exchange Server holding the EDGE role. Without the switch servers holding the EDGE role are excluded.
Include the HTTPERR log files in the purge routine. Those logs are normally stored at C:\Windows\System32\LogFiles\HTTPERR.
Determine the Exchange install path by querying the server object in AD configuration partition. This helps if your Exchange servers do not have a unified install path across all servers.
Absolute path to a repository folder for storing copied log files and compressed archives. Preferably an UNC path. A new subfolder will be created for each Exchange server.
Log file copy and archive mode. Possible values
- None = All log files will be purged without being copied
- CopyOnly = Simply copy log files to the RepositoryRootPath
- CopyAndZip = Copy logfiles and send copied files to compressed archive
- CopyZipAndDelete = Same as CopyAndZip, but delete copied log files from RepositoryRootPath
Switch to send an Html report
Email address of report sender
Email address of report recipient
SMTP Server for email report
.\Purge-LogFiles -DaysToKeep 14
Delete Exchange and IIS log files older than 14 days
.\Purge-LogFiles -DaysToKeep 7 -Auto
Delete Exchange and IIS log files older than 7 days with automatic discovery
.\Purge-LogFiles -DaysToKeep 7 -Auto -SendMail -MailFrom postmaster@sedna-inc.com -MailTo exchangeadmin@sedna-inc.com -MailServer mail.sedna-inc.com
Delete Exchange and IIS log files older than 7 days with automatic discovery and send email report
.\Purge-LogFiles -DaysToKeep 14 -RepositoryRootPath \\OTHERSERVER\OtherShare\LOGS -ArchiveMode CopyZipAndDelete`
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.ps1 -DaysToKeep 7 -SendMail -MailFrom postmaster@sedna-inc.com -MailTo exchangeadmin@sedna-inc.com -MailServer mail.sedna-inc.com -UseDynamicExchangePaths -IncludeHttpErr
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.
THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
Written by: Thomas Stensitzki
- My Blog: https://blog.granikos.eu
- Bluesky: https://bsky.app/profile/stensitzki.bsky.social
- LinkedIn: https://www.linkedin.com/in/thomasstensitzki
- YouTube: https://www.youtube.com/@ThomasStensitzki
- LinkTree: https://linktr.ee/stensitzki
For more Office 365, Cloud Security, and Exchange Server stuff checkout services provided by Granikos
- Website: https://www.granikos.eu/en/
- Bluesky: https://bsky.app/profile/granikos.bsky.social
- Is-Admin function (c) by Michel de Rooij, [email protected]