Avatar of IT CAMPER
IT CAMPER
Flag for United States of America asked on

Add command to script to delete old files

I need to add the function to delete any files older than 300 days in the folders that are read from the csv file.

$scanCsv = 'D:\Folders.csv'
$sendMailArgs = @{
   From = 'email@email.com'
   SmtpServer = 'mail.smtp.com'
   Credential = Import-Clixml -Path "D:\MailCredential.xml"
   UseSsl = $true   # Uncomment to use SSL
   Port = 587   # Uncomment to use SSL
}

Import-Csv -Path $scanCsv | ForEach-Object {
   $fullName = $_.Path -f (Get-Date).AddDays(1).Date.ToString($_.DateTimeFormat)
   Write-Host "Processing '$($fullName)' ($($_.Email))"
   If (-not (Test-Path -Path $fullName)) {
      $body = "Expected file '$($fullName)' not found!"
      Write-Warning $body
      Send-MailMessage @sendMailArgs -To $_.Email.Split(',').Trim() -Subject $_.Subject -Body $body
   } Else {
      Write-Host "Expected file '$($fullName)' found."
   }
}

Open in new window

Powershell

Avatar of undefined
Last Comment
Binh Win

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Binh Win

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23