Search in Files with PowerShell

Search in files with PowerShell and return files, line numbers.

The script in this article will allow you to enter a search directory and the value you’re looking for. As the script finds files that contain your search, the file name, line number, and line content will be displayed.

After this script was created, I later found that select-string was returning parts of words, instead of only the full word: returning “Description” when the search value was “script.” To correct this, this regex bit was used in the pattern:   -Pattern "\b($String_Search)\b", surrounding the variable with “\b(” and “)\b” …. \b being the beginning and end of the word and parentheses specifying a group of characters (or word, in this case).

Powershell Search in Files

 

Leave a Reply

Your email address will not be published.