Powershell script issue

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
July 30th, 2015 2:52pm

You cannot assign two values for extension.  I added an additional variable for $Extension2 and added it to the include statement.  This will get the files in both folders.  It will however check for both extennsions in both folders.  This may be OK based on your fiolder contents.  If the folders contain similar files and you want to only search for one extension in each folder you will need to seperate the get-childitem searches.

Hope this helps

 
#----- 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"
$Extension2 = "*.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,$Extension2 -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"
        }
    }

  • Proposed as answer by jrv Thursday, July 30, 2015 3:29 PM
Free Windows Admin Tool Kit Click here and download it now
July 30th, 2015 3:23pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics