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.


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"


To view the help file of the command run.

Get-Help Select-String -Online