How to search a string in multiple files and return the names of files in Powershell?

Bluz picture Bluz · Nov 16, 2011 · Viewed 384.1k times · Source

I have started learning powershell a couple of days ago, and I couldn't find anything on google that does what I need so please bear with my question.

I have been asked to replace some text strings into multiple files. I do not necessarily know the extension of the possible target files and I don't know their location either. So far I have managed to recursively browse into the directory (get-ChildItem -recurse) and find the string I was looking for with get-content and select-string:

Get-ChildItem -recurse | Get-Content | Select-String -pattern "dummy"

The problem is, I can see the occurences of the text I am looking for, but I don't know how to tell PS to return the path and the name for every matching files as well.

How can I get the name and location of the files that contains the expression I am looking for?

Answer

jon Z picture jon Z · Nov 16, 2011

This should give the location of the files that contain your pattern:

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