Multiple parameters for file searchiing

Hello everyone,
I am trying to modify this script so it looks for multiple file names that starts with certain fashion
Below script works perfectly but I am trying to add one more file name after ‘AR Down’ portion
Get-ChildItem $Path |where {$.Name.Startswith(‘AR Down’)} | Where-Object {$.LastWriteTime -lt $DeleteDate} | Remove-Item -whatif

This is what I want to do:
Get-ChildItem $Path |where {$.Name.Startswith(‘AR Down’) or (‘XXXX’) or {YYYY}} | Where-Object {$.LastWriteTime -lt $DeleteDate} | Remove-Item -whatif

Is this possible?

Hi Korhan,

Yes, this is possible. They key thing to bear in mind that you need to qualify the additional statements.

Also, when you’re using multiple conditions, I recommend that you isolate each condition within brackets. Brackets always take precedence over other parts of an equation, and makes it easier to understand what’s happening without worrying how the answer to the conditions is being calculated.

Something like below should help:

Get-ChildItem $Path |
Where-Object -FilterScript {
    ($_.Name.Startswith('AR Down')) -or ($_.Name.Startswith('XXXX')) -or ($_.Name.Startswith('YYYY'))
} |
Where-Object -FilterScript {
    $_.LastWriteTime -lt $DeleteDate
} |
Remove-Item -WhatIf

Tim,
Thank you so much. Works perfect. Just another noob question if it is not too much to ask
How did you set your code like that? So easy to read. I would like my code to be displayed that way too. It looks so neat.

The second solution below may be faster depending on number of folders and files. Both will work.

Get-ChildItem $Path | Where-Object {$_.Name -match '^(AR Down|XXXX|YYYY)' -AND $_.LastWriteTime -lt $DeleteDate} | Remove-Item -WhatIf
$files = (Get-ChildItem $Path) -match '^(AR Down|XXXX|YYYY)'
$files | Where-Object {$_.LastWriteTime -lt $DeleteDate} | Remove-Item -WhatIf