Skip to content

Instantly share code, notes, and snippets.

@rodolfofadino
Last active August 29, 2015 14:15
Show Gist options
  • Save rodolfofadino/1170ca2b631f2cefceb6 to your computer and use it in GitHub Desktop.
Save rodolfofadino/1170ca2b631f2cefceb6 to your computer and use it in GitHub Desktop.
Delete Older Files
$Now = Get-Date
$Days = "7"
$TargetFolder = "C:\Applications\Logs"
$Extension = "*.log"
$LastWrite = $Now.AddDays(-$Days)
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files)
{
if ($File -ne $NULL)
{
write-host "Deleting File $File" -ForegroundColor "DarkRed"
Remove-Item $File.FullName | out-null
}
else
{
Write-Host "No more files to delete!" -foregroundcolor "Green"
}
}
## See more at: http://www.networknet.nl/apps/wp/published/powershell-delete-files-older-than-x-days#sthash.r7Gj8Dgz.dpuf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment