# Script to search with patterns from file

**URL:** <https://forums.powershell.org/t/script-to-search-with-patterns-from-file/17350>\
**Category:** PowerShell Help\
**Created:** [August 19, 2021, 6:17pm UTC](https://forums.powershell.org/t/script-to-search-with-patterns-from-file/17350 "2021-08-19T18:17:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wire.vipers](https://avatars.discourse-cdn.com/v4/letter/w/ac91a4/32.png) [@wire.vipers](https://forums.powershell.org/u/wire.vipers)\
**Post date:** [August 19, 2021, 6:17pm UTC](https://forums.powershell.org/t/script-to-search-with-patterns-from-file/17350/1 "2021-08-19T18:17:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 19, 2021, 6:30pm UTC](https://forums.powershell.org/t/script-to-search-with-patterns-from-file/17350/2 "2021-08-19T18:30:44Z")

</div>

wire.vipers.0,

Welcome to the forum. 👋🏽

> [@wire.vipers](#):
>
> How to load patterns from text file and do the search recursively ?

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:

```auto
$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:

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

```

---

<div class="post-metadata">

**Author:** ![wire.vipers](https://avatars.discourse-cdn.com/v4/letter/w/ac91a4/32.png) [@wire.vipers](https://forums.powershell.org/u/wire.vipers)\
**Post date:** [August 21, 2021, 12:54am UTC](https://forums.powershell.org/t/script-to-search-with-patterns-from-file/17350/3 "2021-08-21T00:54:56Z")

</div>

@Olaf Thank you very much !

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:21pm UTC](https://forums.powershell.org/t/script-to-search-with-patterns-from-file/17350/4 "2024-05-16T20:21:58Z")

</div>


