Whether you’re a developer, system administrator, or just someone working with large sets of files, finding specific text inside files on Windows can be a challenge. While Windows Explorer allows simple searches, it’s often slow and limited, especially when you need to search recursively across many folders. This is where PowerShell comes to the rescue.

In this guide, we’ll show you how to search for specific text in files across a folder and its subfolders using a single PowerShell command. We'll also demonstrate how to save the results, including both the file path and the line containing the text, to a text file for easy reference.

 

Why Use PowerShell?

PowerShell is a powerful command-line shell and scripting language built into Windows. Unlike basic file searches, PowerShell allows you to:

  • Search recursively in all subfolders

  • Filter by file types (e.g., .txt, .php, .log)

  • Display both the file path and the content line where the text appears

  • Save results to a text file for later use

 

Step-by-Step Guide

1. Open PowerShell

  • Press Win + X and select Windows PowerShell (or Windows PowerShell (Admin) for full access).

  • Alternatively, press Win + S, type PowerShell, and press Enter.

2. Prepare Your Search Command

Here’s an example PowerShell command that searches for multiple text strings in all files under a specific folder and saves the results to a text file:

$SearchPath="C:\Path\To\Your\Folder"; $Patterns=@("Text to find 1","Text to find 2","Text to find 3"); Get-ChildItem $SearchPath -Recurse -File -Include *.* | ForEach-Object { foreach ($p in $Patterns) { $match=Select-String -Path $_.FullName -Pattern $p -SimpleMatch -ErrorAction SilentlyContinue; if($match){$match | ForEach-Object {"$($_.Path): $($_.Line)"}} } } | Sort-Object -Unique | Out-File "C:\Path\To\Your\Folder\SearchResults.txt"

 

3. How This Command Works

$SearchPath — Set this to the folder where you want to search.
$Patterns — An array of all text strings you want to find. You can add as many strings as needed.
Get-ChildItem -Recurse -File -Include *.* — Recursively lists all files in the folder.
Select-String -Pattern $p -SimpleMatch — Searches for the text string in each file.
"$($_.Path): $($_.Line)" — Formats the output to show the full file path and the line containing the match.
Sort-Object -Unique — Removes duplicate entries.

4. Check Your Results

After running the command, open the SearchResults.txt file. Each line will show the file path followed by the matching line, for example:

 
C:\Path\To\Your\Folder\example.txt: This is the line containing the text you searched for.
C:\Path\To\Your\Folder\subfolder\anotherfile.php: Another matching line with your text.

This makes it easy to locate exactly where your text appears, even in large projects.

  • Out-File — Saves the output to a text file for later review.

5. Tips for Effective Searches

  • To search only specific file types, change the -Include parameter, e.g., -Include *.php,*.txt.

  • If you need case-insensitive search, -SimpleMatch is already case-insensitive by default.

  • For very large folders, consider narrowing the search path to improve speed.

Conclusion

Using PowerShell to search for text in files is much faster and more flexible than using Windows Explorer. With just one command, you can search recursively, find multiple strings, and save the results with file paths and content lines. This approach is ideal for developers, Moodle administrators, or anyone who works with large sets of files regularly.