Use PowerShell to search file content on windows

In this blog, I will show you how to use powershell Select-String cmdlet to string or words from multiple files on Windows quickly?

Select-String is based on lines of text. By default, Select-String finds the first match in each line and, for each match, it displays the file name, line number, and all text in the line containing the match.

Find matches in special files

This command searches all files with the .php file name extension in the current directory

select-string *.php -pattern function

The output displays the lines in those files that include the specified string.

Find a string in subdirectories

This example searches a directory and all of its subdirectories for a specific text string.

Get-ChildItem -Path C:\inetpub\wwwroot\wordpress -Include *.php -Recurse | Select-String -Pattern ‘function posted_on’

Get-ChildItem uses the Path parameter to specify folder, The Include parameter indicate the file type will be searched The Recurse parameter includes the subdirectories. The objects are sent down the pipeline to Select-String.

Select-String uses Pattern parameter and specifies the string ‘ function posted_on ‘

Find a pattern match with wildcard

Select-String -Path ” C:\inetpub\wwwroot\wordpress\*.php” -Pattern ‘posted_on*’