Using Powershell to Keep Directories Clean

As an integration engineer I have log files that provide me an audit trail so I can track things down when they go astray.  Now this is fine and well but what happens when you’ve had an integration in place since 2007 and never needed to look at those files?  Yes you’ve got 5 years worth of files that are taking up space on the drive.  To alleviate the issue you can use Powershell to clear the directories of any files over a certain age.  Here is the code on how to do it:

# Delete all Files in the TCC Archives over 60 days old
$Path = "C:\Taleo\TCC\Prod\Export\AirlandRequisition\Archive"
$Daysback = "-60"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

clear-variable -name Path
clear-variable -name Daysback
clear-variable -name CurrentDate
clear-variable -name DatetoDelete

$Path = "C:\Taleo\TCC\Prod\Export\AppTrackCSW\Archive"
$Daysback = "-60"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

clear-variable -name Path
clear-variable -name Daysback
clear-variable -name CurrentDate
clear-variable -name DatetoDelete

Comments are closed.