Hello,
I've got a script that I found online that nearly does everything I need it to do. It runs as a scheduled task on servers and looks at a specific directory for files with a specific extension and deletes them if they are older than 2 days. My problem is that I need to add another specific directory and a corresponding extension type. Any ideas what I'm doing wrong? The initial $TargetFolder works, but the second one does not. I'm not at a a scripting/PS expert, so any help would be appreciated.
#----- define parameters -----# #----- get current date ----# $Now = Get-Date #----- define amount of days ----# $Days = "2" #----- define folder where files are located ----# $TargetFolder = "C:\Apps" $TargetFolder2 = "C:\Try" #----- define extension ----# $Extension = "*.log,*.TRY" #----- define LastWriteTime parameter based on $Days ---# $LastWrite = $Now.AddDays(-$Days) #----- get files based on lastwrite filter and specified folder ---# $Files = Get-Childitem $TargetFolder,$TargetFolder2 -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" } }
- Edited by nwebb98 Thursday, July 30, 2015 2:55 PM