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?
This should give the location of the files that contain your pattern:
Pipe the content of your
to
fl *
You will see that the path is already being returned as a property of the objects.
IF you want just the path, use
select path
orselect -unique path
to remove duplicates:There are a variety of accurate answers here, but here is the most concise code for several different variations. For each variation, the top line shows the full syntax and the bottom shows terse syntax.
Item (2) is a more concise form of the answers from Jon Z and manojlds, while item (1) is equivalent to the answers from vikas368 and buygrush.
List FileInfo objects for all files containing pattern:
List file names for all files containing pattern:
List FileInfo objects for all files not containing pattern:
List file names for all files not containing pattern:
This will give you the full details of all files
This will display the path, filename and the content line it found that matched the pattern.
This is how I would do it, you don't need get-content:
or
To see what the different properties are...