Help with a simple script...

I am pretty new to powershell and love it! I wrote this little snippet and it works, only under the condition you are at the console and click yes to all when it says hey you did not use the recurse option for directory X … I just do not get where i missed adding a recurse flag at. Any insight is appreciated.

$limit = (Get-Date).AddDays(-14)
$paths = @("F:\specific\place\for\tomcat\temp\","F:\another\place\for\log\archives\")
$excluded = @("*.pid", "*.status", "*.lock")

# Delete files that are not $excluded and older than the $limit.
 
foreach ($path in $paths)
    {

Get-ChildItem -Path $path -Recurse -Force -Exclude $excluded | Where-Object { $_.CreationTime -lt $limit } | Remove-Item -Force

    }

If you do it like this: Remove-Item -Confirm:$false
This stops the commmand from asking for permission.

it sounds like that is coming from remove-item. Do you have any subfolders in any of those paths? if so, your loop will send every subfolder down the pipeline, so at some point you’re effectively trying to delete a subfolder. That’s when Remove-item will send you that message.

Alternately if you don’t want to delete any subfolders you could add the “-file” switch parameter to your “get-childitem” command. That will retrieve only files and should avoid any questions about recursion from remove-item. It would leave empty subfolders if you have any though, so not sure if that’s what you’re after.

You can also add “-confirm:$False” to “Remove-Item” but be careful with that combination. it’s a good way to delete alot of stuff really quickly

You’ll want to be careful with that. You didn’t tell Get-ChildItem to only return files, so if it finds a directory that matches your Where-Object criterium then you will delete it and its contents even if the files inside of it do not match the criterium.

Historically. I’ve used this approach for such efforts…
Based on the sample provided, I’d would do stuff like this…

foreach ($path in $paths)
{

    ForEach ($TargetFilename in (Get-ChildItem -Path $path -Recurse -Force -Exclude $excluded | 
                Where-Object { $_.CreationTime -lt $limit }).FullName)
    {
       If (($TargetFilename) -notmatch $excluded)
       { Remove-Item -Path $TargetFilename -Confirm:$false -Force }
    }
}

Frankly, I gotten into the habit that when I have to do such things like the query here. I use Move-Item to a folder named ReviewBeforeDeletion.

foreach ($path in $paths)
{

    ForEach ($TargetFilename in (Get-ChildItem -Path $path -Recurse -Force -Exclude $excluded | 
                Where-Object { $_.CreationTime -lt $limit }).FullName)
    {
       If (($TargetFilename) -notmatch $excluded)
       { 
            # Remove-Item -Path $TargetFilename -Confirm:$false -Force
            Move-Item -Path $TargetFilename -Destination 'H:\ReviewBeforeDeletion' -Confirm:$false -Force
       }
    }
}

So I still get rid of the goods in a given folder that I do not want there, but have a temp backup before finalizing the delete. This ensures I get to physically see that I am only getting files. If I get a folder, for whatever reason, I can always move that back.

Thanks for the reply, in this instance its strictly a disk space maintenance effort and our redundancy in place keeps everything on demand for 30 days so if something that shouldn’t have been deleted does get deleted I can get it back in a matter of moments. That being said I will definitely keep this approach on hand for other situations where I may want to do some eyeball confirmations.

Thanks for the reply, with the redundancy protection we have in place and this being a temp directory over two weeks old I was not too worried about blowing out everything. But yeah not an option id put in place just anywhere.

Thanks for the reply, in this scenario the “-confirm:$False” pretty much hit the nail on the head. However the application this is helping me keep maintenance on is being upgraded next month and instead of a temp directory bucket the temp files will be sorted down into individually pre-created directories for their associated plugins, so I will absolutely be using the -file switch on that new server.

Thanks alot that did the trick for the time being!