- Blog - http://blog.bennett-scharf.com -
PowerShell Script to Clean Up Old Files Based on Age
Posted By admin On 20. January 2010 @ 12:58 In Utilities, Windows OS | 3 Comments
Here’s an extremely concise PowerShell script to remove old files. You could use it to enforce a policy that keeps 90 days of ETL input files on a server. Files older than that would purged with this script. The script would be invoked by a SQLAgent CMDExec task or a Windows Task Scheduler job.
#Powershell Script to delete files older than a certain age
$intFileAge = 90 #age of files in days
$strFilePath = “c:\archive” #path to clean up
#create filter to exclude folders and files newer than specified age
Filter Select-FileAge
{
param($days)
If ($_.PSisContainer) {}
# Exclude folders from result set
ElseIf ($_.LastWriteTime -lt (Get-Date).AddDays($days * -1))
{$_}
}
get-Childitem -recurse $strFilePath | Select-FileAge $intFileAge ‘CreationTime’ | remove-item
Article printed from Blog: http://blog.bennett-scharf.com
URL to article: http://blog.bennett-scharf.com/2010/01/20/powershell-script-to-clean-up-old-files-based-on-age/
Click here to print.