This script can be used to perform file deletions based on age in days, and with the option to send an email summary report. In my particular case, this is deployed to performed housekeeping of IIS and Exchange Server Logs older than seven (7) days. This is a fork of the previous script published as DeleteFilesOlderThanXDays.ps1
This accepts the directory or path that will be the target of the cleanup. It accepts multiple entries separated by a comma. The paths can be a local or UNC path location.
Example: "C:\TEMP","\\SERVER\C$\TEMP"
-outputDirectory
Indicates the path where the output files will be saved.
Example: "C:\Scripts\Output"
-include
Single or List of files or file extensions to include in the deletion.
Example: "*.log","file1.txt",*.blg" - This will delete files with specific extensions or file names.
Example: "*.*" THIS WILL DELETE ALL FILES
-daysToKeep
Files that are older than this number of days will be deleted.
Example: 30 - This will keep the files modified within the last 30 days.
-exclude
Single or List of files or file extensions to exclude from the deletion.
Example: "*.log","file1.txt",*.blg"
-recurse
Switch to indicate whether or not a recursive deletion will be performed. No value is required for this parameter.
-sendEmail
Switch to indicate whether or not to send the HTML summary report through email. No value is required for this parameter.
-sender
Sender Email Address to be used for the Summary Report. Required if sendEmail switch is used.
Example: sender@domain.com OR "Sender Display Name"
-recipients
List of recipients of the email report. Multiple recipients are accepted separated with a comma. Required if sendEmail switch is used.
Example: "recipient1@domain.com","recipient2@domain.com"
-smtpServer
The SMTP Relay Server. This can be an IP Address, Hostname or FQDN. Required if sendEmail switch is used.
Example: 192.168.0.6
Example: smtp.server.com
-smtpPort
SMTP Server Port to use. This is normally port 25 for an unauthenticated relay, or 587 for Office 365 authenticated relay. Required if sendEmail switch is used.
Example: 587
-smtpSSL
Switch to indicate whether or not SSL will be used for the SMTP relay connection. No value is required for this parameter.
-smtpCredential
Used if SMTP relay (eg. Office 365) requires authentication. Required if sendEmail switch is used.
-notifyTeams
This can be used to send the notification to one or more MS Teams Webhook.
Requirements
- PowerShell 5.1 (recommended), but works and tested with PowerShell 4.
- If "email summary" report will be enabled, an SMTP relay is required.
Script Installation
From GitHub
To get the script, you can clone or download from this link - Delete-FilesByAge.ps1 and save it on your preferred path.
From PowerShell Gallery
If you have PowerShell 5, you can simply install from PSGallery.
https://www.powershellgallery.com/packages/Delete-FilesByAge
https://www.powershellgallery.com/packages/Delete-FilesByAge
How to Use
Required Parameters
-PathsThis accepts the directory or path that will be the target of the cleanup. It accepts multiple entries separated by a comma. The paths can be a local or UNC path location.
Example: "C:\TEMP","\\SERVER\C$\TEMP"
-outputDirectory
Indicates the path where the output files will be saved.
Example: "C:\Scripts\Output"
-include
Single or List of files or file extensions to include in the deletion.
Example: "*.log","file1.txt",*.blg" - This will delete files with specific extensions or file names.
Example: "*.*" THIS WILL DELETE ALL FILES
-daysToKeep
Files that are older than this number of days will be deleted.
Example: 30 - This will keep the files modified within the last 30 days.
Optional Parameters
-logDirectory
Indicates the path where the transcript log files will be saved.
Example: "C:\Scripts\Logs"
-exclude
Single or List of files or file extensions to exclude from the deletion.
Example: "*.log","file1.txt",*.blg"
-recurse
Switch to indicate whether or not a recursive deletion will be performed. No value is required for this parameter.
-sendEmail
Switch to indicate whether or not to send the HTML summary report through email. No value is required for this parameter.
-sender
Sender Email Address to be used for the Summary Report. Required if sendEmail switch is used.
Example: sender@domain.com OR "Sender Display Name
-recipients
List of recipients of the email report. Multiple recipients are accepted separated with a comma. Required if sendEmail switch is used.
Example: "recipient1@domain.com","recipient2@domain.com"
-smtpServer
The SMTP Relay Server. This can be an IP Address, Hostname or FQDN. Required if sendEmail switch is used.
Example: 192.168.0.6
Example: smtp.server.com
-smtpPort
SMTP Server Port to use. This is normally port 25 for an unauthenticated relay, or 587 for Office 365 authenticated relay. Required if sendEmail switch is used.
Example: 587
-smtpSSL
Switch to indicate whether or not SSL will be used for the SMTP relay connection. No value is required for this parameter.
-smtpCredential
Used if SMTP relay (eg. Office 365) requires authentication. Required if sendEmail switch is used.
-notifyTeams
This can be used to send the notification to one or more MS Teams Webhook.
Example
In this example:
- Using Office 365 as SMTP Relay, with Authentication, using Port 587 with SSL enabled.
- Stored Authentication credential in variable $credential
- Included *.log,*.blg extensions.
- Excluded log1.log
- Files older than 30 days will be deleted.
- Selected Paths "\\poshlabexc\c$\inetpub\logs","c:\temp"
Command:
$credential = Get-Credential .\Delete-FilesByAge.ps1 -Paths "\\poshlabexc\c$\inetpub\logs","c:\temp" ` -Include *.log,*.blg -Recurse -daysToKeep 30 ` -outputDirectory "C:\Scripts\Delete-FilesByAge\output" ` -headerPrefix LazyExchangeAdmin ` -sendEmail ` -sender postmaster@lazyexchangeadmin.com` -recipients june@lazyexchangeadmin.com ` -smtpServer smtp.office365.com ` -smtpPort 587 ` -smtpCredential $credential ` -smtpSSL ` -logDirectory "C:\Scripts\Delete-FilesByAge\Log" ` -Exclude log1.log