ShiftNick

Remove Files Older Than 15 Days

Mar 1st, 2015
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3. Deletes unwanted files from sever
  4. .DESCRIPTION
  5. Deletes all .zip files that are older than 15 days from the location specified in the $Path variable and emails a file containing all deleted items
  6. .PARAMETERS
  7.  
  8. .EXAMPLE
  9.  
  10. #>
  11.  
  12. $Path = "\\[SERVERNAME]\c$\Program Files\Software\Backup"
  13. $Date = (Get-date).AddDays(-15)
  14. $Files = Get-ChildItem -path $Path -include *.zip -recurse | where { ($_.CreationTime) -le $Date}
  15. $SMTPServer="mailserver.domain.com"
  16. $From = "Email Address<[email protected]>"
  17. $Recipient = "Email Address<[email protected]>"
  18. $logFile = "c:\temp\FilesDeleted.txt"
  19. $testing = "Disabled" # Set to Disabled to Email Users
  20. $testRecipient = "[email protected]"
  21.  
  22. #Outputs a file that contains the logs that are to be deleted
  23.  
  24. $Files | Out-File  "c:\temp\FilesDeleted.txt"
  25.  
  26. #Performs the delete of the files
  27.  
  28. $Files | Remove-Item -Force
  29.  
  30. #Emails $Recipient with the log file attached
  31.        # If Testing Is Enabled - Email Administrator
  32.     if (($testing) -eq "Enabled")
  33.     {
  34.         $Recipient = $testRecipient
  35.     } # End Testing
  36.  
  37. # Email Subject Set Here
  38.     $subject="Backups were deleted"
  39.  
  40. # Email Body Set Here, Note You can use HTML, including Images.
  41.     $body ="<font face=Arial>
  42.    Hello,
  43.    <p>The backups that are 15 days or older were successfully deleted<br>
  44.    <p>Thanks, <br>
  45.    <p> The Ghost in the machine!<br>
  46.    </font>
  47.    </P>"  
  48.          
  49. # Send Email Message
  50. Send-Mailmessage -smtpServer $smtpServer -from $from -to $Recipient -subject $subject -body $body -Attachments $logfile -bodyasHTML -priority High
Advertisement
Add Comment
Please, Sign In to add comment