Delete Files Older than 90 Days with Powershell

Here is a powershell script that has been stolen, chopped up and modified.
Its logs the changes to Windows Event Viewer (because log files are so yesterday!) and removes files more than 90 days old from the directories specified.

# Script that deletes files older than 90 days in the E:Data directory.

Function Write-Log {

	$evt=new-object System.Diagnostics.EventLog("Application")
	$evt.Source="Scans Directory Cleanup"


Function RemoveFilesAndDirs {


    foreach ($path in $Paths){
        $files = (Get-ChildItem $path -recurse | where {$_.Lastwritetime -lt $TargetDate})
        Write-Log "Attempting to remove old files on $path"
        # Test if we are really moving files
        try {
            foreach ($file in $files){
                Write-Log ($file.fullname).ToString()
        catch [System.Management.Automation.RuntimeException] {
            $re = 1
            Write-Log "Nothing to delete"
        if($re -ne 1){
            Get-ChildItem $path -recurse | where {$_.Lastwritetime -lt $TargetDate} | remove-item -recurse

$today = Get-Date -displayhint date
$targetdate = ($today.AddDays(-90))

$path = "E:/Data"
$directories = @("$path/temp","$path/scratch","$path/dev-temp","$path/rubbish")

Write-Log "Starting cleanup"
Write-Log "Files older than $targetdate will be deleted"

RemoveFilesAndDirs $directories $targetdate

Write-Log "*** Done ***"