Search For String Inside Any File With PowerShell

This PowerShell blog post will show how to use PowerShell to search for a string or pattern inside any file with PowerShell.

In the following example, I have a test file that contains three lines of text. Using PowerShell select-string I can scan the entire file for a specific string.

Text File

Below is my text file that contains three lines of text.

ntweekly.com
com
myname

Search

At the most basic form, I can use the command as follow and print the result to the screen.

Select-String -Path .\file.txt -Pattern "myname"

The following cmdlets will search the text file for the string myname. It also saves the results to a variable and print the line number the match is found, the actual line and the details of the result.

$search = Select-String -Path .\file.txt -Pattern "myname"

$search.LineNumber
$search.Line
$search.Matches

To view the help file of the command run.

Get-Help Select-String -Online

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.