Script to search with patterns from file

Newbie … I have a text file with patterns to search folder and sub-folders. How to do that ?
For a simple search this works :
Get-ChildItem -Path C:\WINFILES\ -Recurse | Select-String -Pattern ‘ABC’
How to load patterns from text file and do the search recursively ?
Appreciate any help.

wire.vipers.0,

Welcome to the forum. :wave:t4:

That depends pretty much on the complexity and the amount of patterns you have and the result you’re expecting.

If you want to search the whole directory again and again for each single pattern you could use a loop.
Assumed you have a single pattern per line in your text file you could start with something like this:

$PatternList = Get-Content -Path '.\$PatternList.txt'
foreach ($Pattern in $PatternList) {
    Get-ChildItem -Path C:\WINFILES\ -Recurse | Select-String -Pattern $Pattern
}

BTW: When you post code or sample data or console output please format it as code using the preformatted text button ( </> ).

Thanks in advance

Edit:

Looking up the syntax to be sure I just noticed that you don’t actually need a loop. The parameter -Pattern takes an array. So it would be something like this then:

$PatternList = Get-Content -Path '.\$PatternList.txt'
Get-ChildItem -Path C:\WINFILES\ -Recurse | Select-String -Pattern $PatternList
1 Like

@Olaf Thank you very much !