Last active
August 29, 2015 14:15
-
-
Save rodolfofadino/1170ca2b631f2cefceb6 to your computer and use it in GitHub Desktop.
Delete Older Files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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