A PowerShell script to compress and manage log files by archiving and optionally deleting them after a retention period. Designed for Windows environments, this script is ideal for managing IIS and other application logs, helping you save disk space and maintain tidy log directories.
- Compresses log files older than a specified number of days into
.ziparchives. - Deletes original log files after archiving (unless
-ArchiveOnlyis specified). - Deletes old archives: Removes archived ZIP files older than a configurable retention period.
- Supports NTFS decompression: Optionally decompresses NTFS-compressed files before deletion to reclaim full logical space.
- Flexible archive location: Archives can be stored in an
Archivesubfolder within each source folder or in a global destination. - Robust logging: Logs actions and errors to a log file and the Windows Event Log.
- Progress reporting: Displays progress and summary information during execution.
- Safe and idempotent: Handles errors gracefully and checks permissions before making changes.
- Windows PowerShell 5.0 or later (requires
Compress-Archivecmdlet) - Sufficient permissions to read, write, and delete files in the source and destination folders
.\CompressAndDeleteLogs.ps1 -SourcePath "C:\inetpub\logs\LogFiles" -RetentionDays 30 -DecompressBeforeDeleteParameters:
-
-SourcePath
The root folder to search for log files. -
-DestinationPath
(Optional) The root folder where compressed files will be stored. If not specified, uses anArchivesubfolder in each source folder. -
-LogFilePath
(Optional) The path to the log file. Defaults toCompressAndDeleteLogs.login the script folder. -
-RetentionDays
The number of days to retain log files before compressing and deleting. -
-ArchiveRetentionDays
The number of days to retain archived ZIP files before deleting them from the archive folder. -
-ArchiveOnly
If set, archives logs but does not delete the originals. -
-DecompressBeforeDelete
If set, decompresses NTFS-compressed files before deletion to free up the full logical size.
Archive and delete logs older than 30 days, decompressing NTFS-compressed files before deletion:
.\CompressAndDeleteLogs.ps1 -SourcePath "C:\inetpub\logs\LogFiles" -RetentionDays 30 -DecompressBeforeDeleteArchive logs to a global destination and log to a custom file:
.\CompressAndDeleteLogs.ps1 -SourcePath "C:\inetpub\logs\LogFiles" -DestinationPath "E:\Logs" -LogFilePath "D:\Scripts\CompressAndDeleteLogs.log" -RetentionDays 30- Actions and errors are logged to the specified log file (default:
CompressAndDeleteLogs.log). - Critical errors are also written to the Windows Event Log under the source
CompressAndDeleteLogs.
- Ensure you have appropriate permissions to run the script and modify files in the specified directories.
- The script is safe to run multiple times; it will not re-archive or re-delete files unnecessarily.
- 1.1.0 (2025-06-03): Major refactor with modular functions, improved logging, NTFS decompression support, archive retention, and robust error handling.
MIT Licence
Author: Chris McMorrin
For more information or to report issues, please use the repository's issue tracker.