How to Find Text in Multiple Files Using PowerShell

Use Select-String in Windows PowerShell when you need to find a word or pattern across several text files without opening them individually.

Search multiple text files

For example, to find the text ayam in the DEFAULT1.txt through DEFAULT5.txt files under C:\ITsiti, use the following command. Adjust the path and search text for your environment.

Select-String -Path 'C:\ITsiti\DEFAULT*.txt' -Pattern 'ayam' -SimpleMatch

The result includes the path, line number and matching line. In the original example, the matches were:

DEFAULT1.txt:18:ayam_kampung
DEFAULT2.txt:10:ayam
DEFAULT5.txt:27:ayam_bandar

Search within subfolders

Get-ChildItem -Path 'C:\ITsiti' -Filter '*.txt' -File -Recurse |
    Select-String -Pattern 'ayam' -SimpleMatch

Use -SimpleMatch for literal text. Without it, the pattern is interpreted as a regular expression. Select-String is suitable for plain text files; it is not a parser for binary Word or PDF documents. Limit the search to authorized folders, especially when looking through application logs.

Reference

Microsoft Learn: Select-String

You May Also Like

Leave a Reply?