Find text in files using Powershell

To find a string inside files in a directory tree with PowerShell, move to the root of the desired directory and run:

Get-ChildItem -Recurse -Filter *.config | Select-String -Pattern "find_for_this_string"

This searches for find_for_this_string inside files of type *.config (e.g. web.config files), recursively from the current directory.

How it works

  • Get-ChildItem enumerates files; -Recurse walks subdirectories and -Filter matches a single wildcard pattern (much faster than -Include, and without its wildcard-path quirks).
  • Select-String is the PowerShell equivalent of grep: it searches file contents for a pattern and outputs the match, line number, and filename.

Reading this in 2026

The command still works unchanged on PowerShell 7.x on Windows, Linux, and macOS (the modern -Filter form above is the one to memorize). A few refinements worth knowing:

  • Drop the wildcard dance: modern Get-ChildItem -Recurse -Filter *.config is cleaner and faster than .\* plus -Include.
  • Search everything: omit -Include/-Filter entirely to search all files; add -SimpleMatch to Select-String when your pattern is a literal string, not a regex.
  • Case sensitivity: Select-String is case-insensitive by default; use -CaseSensitive when that matters.
  • On Linux/macOS, grep -r "find_for_this_string" --include='*.config' . remains the native habit — but PowerShell’s cross-platform support means the same muscle memory works everywhere.

Leave a Reply

Your email address will not be published. Required fields are marked *