This script will delete files older than 10 days.
$Now = Get-Date
$LastWrite = $Now.AddDays(-10)
$Files = get-childitem -Path “C:\folder_with_files_to_delete” |Where {$_.CreationTime -le $LastWrite}
foreach ($File in $Files)
{Remove-Item -Path $File -Force}
Save this script as “c:\scripts\deleteoldfiles.ps1”
In schedule task select program/script to run : “C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe”
and use : -command “c:\scripts\deleteoldfiles.ps1” as argument
Remember you has to set the execution policy to allow this script to run. (set-executionpolicy -executionpolicy remotesigned)
Update:
For PowerShell 1.0 and filter on file Attribute:
# Written by Atle Vatland @ Cegal AS
# Remember to change path,attribute and fileextension.
# When tested OK – remove WHATIF from Remove-item and the files will be deleted.
# Schedule a task.
# Save this script as “c:\scripts\filename.ps1”
# In schedule task select program/script to run : “C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe”
# and use : -command “c:\scripts\filename.ps1” as argument
# Remember you has to set the execution policy to allow this script to run. (set-executionpolicy -executionpolicy remotesigned)
$Now = Get-Date
$LastWrite = $Now.AddDays(-14)
$Files = get-childitem -Path “E:\filepath\*” -include *.ext |Where {$_.LastWriteTime -le $LastWrite}
$Today= ” ———================== Files deleted ” + $Now + “====================——————–”
$Today | Out-File -filepath c:\scripts\deletedfiles.log -Append
foreach ($File in $Files)
{
$FI=get-item $File
if($FI.Attribute -notlike “*Archive*”){
$File | ft -HideTableHeaders | out-file -filepath c:\scripts\deletedfiles.log -Append;remove-item $File -whatif
}
}