2

I was wondering if is possible to specify multiple search filters as once. For example, I have this line of code that finds all files that have the "&" symbol.

get-childitem ./ -Recurse -Filter "*&*" | 
    ? { $_.PSIsContainer } | 
    Select-Object -Property FullName

I'd like to extend this so that I can search one time and find files with other symbols like %,$,@, etc. I want to find files that have any of these symbols, not neccesarly files that have all of them so I assume there needs to be an OR somewhere. I tried the below code but it didn't seem to work for me:

get-childitem ./ -Recurse -Filter "*&*" -Filter "%" | 
    ? { $_.PSIsContainer } | 
    Select-Object -Property FullName

2 Answers 2

2

You can use the -match operator and a regex for this:

Get-ChildItem -Recurse | 
    Where { !$_.PSIsContainer -and ($_.name -match '&|%|\$|@')} | 
    Select-Object -Property FullName

If you are on PowerShell v3 or higher you can simplify this a bit:

Get-ChildItem -Recurse -File | 
    Where Name -match '&|%|\$|@' | 
    Select-Object -Property FullName
Sign up to request clarification or add additional context in comments.

2 Comments

I'd use a character group ([&%$@]) rather than an alternation.
Yes, that would be better. Thanks.
2

If you have V3 or better, you can leverage the "globbing" wildcard feature:

get-childitem './*[&%$@]*' -Recurse | where {$_.PSIsContainer} 

If you've got V4, you can dispense with the $_.PSIsContainer filter and use the -Directory switch:

get-childitem './*[&%$@]*' -Recurse -Directory

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.