Link to home
Create AccountLog in
Avatar of IT CAMPER
IT CAMPERFlag 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

ASKER CERTIFIED SOLUTION
Avatar of Binh Win
Binh Win
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account