I have to cleanup a server share daily. The following powershell script will remove old files and email a note to me telling me it ran.
#
# setup variables
#
$ageInDays = 7
$fileShare = "\\server\share\subfolder"
#
# cleanout $fileShare and leave
#
$ageInDays worth of recent files
#
Get-ChildItem $fileShare -Recurse | where {$_.LastWriteTime -le (Get-Date).AddDays(-$ageInDays)} | Remove-Item -recurse
#
# setup to mail the results
#
$SmtpClient = new-object system.net.mail.smtpClient
$SmtpServer = "smtp.domain.com"$SmtpClient.host = $SmtpServer
$From = new-object System.Net.Mail.MailAddress("scheduledtask@domain.com", "Schedule Task")
$To = "me@domain.com"
$Title = "Snapshot cleanup run"
$Body = "The powershell snapshot cleanup of $fileShare successfully ran.
This left $ageInDays days of snapshots. Script location is '\\server\share\cleanup_snapshots.ps1'"
#
# mail the results
#
$SmtpClient.Send($from,$to,$title,$Body)
I need to get it to mail on failure, but I've not gotten that far.