Skip to content

Instantly share code, notes, and snippets.

@gabrielgreen
Created May 4, 2012 20:11
Show Gist options
  • Save gabrielgreen/2597414 to your computer and use it in GitHub Desktop.
Save gabrielgreen/2597414 to your computer and use it in GitHub Desktop.
powershell script to delete old files and log what gets deleted
$d = "C:\db\backup"
$today = get-date -uformat "%Y_%m_%d"
$log = "C:\db\backup\" + "Purge_" + $today + ".log"
$a = Get-ChildItem $d -recurse
foreach($x in $a)
{
$y = ((Get-Date) - $x.CreationTime).Days
if ($y -gt 3 -and $x.PsISContainer -ne $True)
{
$deleted = "Deleting - " + $x.fullname + " - Last Write Time - " + $x.LastWriteTime
add-content $log -value $deleted
$x.Delete()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment