Delete files based on last modified date
I created the below PowerShell script to delete folders/files that had a last modified date older than 30 days. This is indicated as -ge 30. You can substitute -ge with -le to find files with a last modified date more recent than x number of days. You can also sub .lastwritetime with .CreationTime if you are after the date the file was created.remove-item -force -path "c:\folder\deleted_logs.txt" $target = get-childitem E:\Logs -recurse | where { ((get-date)-$_.lastwritetime).days -ge 30}$target | out-file c:\folder\deleted_logs.txt $target | remove-item -recurse -force
$recipients
= "Nick <nick.beare@company.gov.au>" $cc = "Nick <nick.beare@company.gov.au>" send-mailmessage -to $recipients -cc $cc -from "username <username@company.gov.au>" -subject "folder - removal of logs on \\hostname\Logs" -body "The attached logs have been removed from \\hostname\Logs because their creation date was older than 30 days. If the attachment is empty, no logs have reached this age and therefore none were removed." -smtpserver mailhost.nickbeare.com -attachment "c:\folder\deleted_logs.txt"
Back