Finding a Specific Text Inside Multiple Files Using PowerShell
When working with many .txt and .md files, opening them one by one to locate a specific phrase is slow and inefficient. PowerShell allows you to search inside files recursively and instantly identify where a string appears.
Basic Recursive Search
Use Get-ChildItem together with Select-String:
Get-ChildItem "C:\Users\ABC\Desktop\my-repos\the-path" -Recurse -File |
Select-String -Pattern "Posts.*category"
How it works:
Get-ChildItem -Recurse -Filecollects all files inside the folder and its subfolders.- The pipeline (
|) sends those files toSelect-String. Select-String -Patternsearches inside each file using a regex pattern.
In this example, the pattern Posts.*category matches any text where “Posts” appears before “category”, even if extra words or spaces exist between them.
Show Only the Matching Filenames
If you only need the file paths without preview lines:
Get-ChildItem "C:\Users\ABC\Desktop\my-repos\the-path" -Recurse -File |
Select-String -Pattern "Posts.*category" |
Select-Object -ExpandProperty Path -Unique
Limit the Search to Specific File Types
Get-ChildItem "C:\Users\ABC\Desktop\my-repos\the-path" -Recurse -File |
Where-Object { $_.Extension -in ".txt",".md" } |
Select-String -Pattern "Posts.*category"
This approach is reliable because it avoids issues with -Include filters and works consistently across folders.