PowerShell: How to Find All Files Containing a Specific String


You can use the following syntax in PowerShell to find all files that contain a specific string:

Get-ChildItem -Recurse | Select-String "Mavs" -List | Select Path

This particular example will return the paths for each file in the current directory that contains “Mavs” somewhere in the file.

Note that we use -List to only display the unique file names so that if “Mavs” occurs multiple times in a single file we only display that file path once.

The following example shows how to use this syntax in practice.

Example: How to Find All Files Containing a Specific String in PowerShell

Suppose that our current directory contains the following three text files:

  • team_info1.txt
  • team_info2.txt
  • team_info3.txt

We can use the Get-Content cmdlet to view the contents of each of these files:

Each of the files contain information about various basketball players including the team name each player belongs to and the number of points scored by the player.

Suppose that we would like to get all of the files that contain the pattern “Mavs” somewhere in the file.

We can use the following syntax to do so:

Get-ChildItem -Recurse | Select-String "Mavs" -List | Select Path 

The following screenshot shows how to use this syntax in practice:

PowerShell find all files containing string

This returns the following files:

  • team_info1.txt
  • team_info2.txt

This tells us that the pattern “Mavs” occurs at least once in both of these files.

Feel free to replace “Mavs” with any other pattern that you would like to search for in your own files.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Replace Multiple Strings in File
PowerShell: How to Check if String Exists in List of Strings
PowerShell: How to Replace Special Characters in String
PowerShell: How to Replace Text in String

Leave a Reply