Want more features on Pastebin? Sign Up, it's FREE!
Guest

Delete Files of age n

By: a guest on Nov 15th, 2012  |  syntax: Power Shell  |  size: 0.96 KB  |  views: 199  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #Uses the "not PSIsContainer" to ensure only files are removed, and not subdirectories. Change the location and the log location (or remove entirely)
  2.  
  3. $DeleteOldFiles = get-childitem C:\temp -recurse | where {$_.lastwritetime -lt (get-date).adddays(-8) -and -not $_.psiscontainer} | ForEach-Object {remove-item $_.fullname -force}
  4. $DeleteOldfiles
  5. SendMail
  6. function SendMail
  7. {
  8.  
  9.      #Write-Host "Sending Email"
  10.  
  11.      #SMTP server name
  12.      $smtpServer = "<SMTP SERVER NAME OR ADDRESS>"
  13.  
  14.      #Creating a Mail object
  15.      $msg = new-object Net.Mail.MailMessage
  16.  
  17.      #Creating SMTP server object
  18.      $smtp = new-object Net.Mail.SmtpClient($smtpServer)
  19.          $Content = Get-Content C:\temp\logs.txt
  20.          $Body = Write-Host $Content
  21.          
  22.      #Email structure
  23.      $msg.From = "Email Address"
  24.      $msg.ReplyTo = "Email Address"
  25.      $msg.To.Add("Email Address")
  26.      $msg.subject = "File Deleted"
  27.          $msg.body = $Body
  28.  
  29.      #Sending email
  30.      $smtp.Send($msg)
  31.          
  32. }
clone this paste RAW Paste Data