如何在多个文件中搜索字符串并在Powershell中返回文件名?

问题描述:

几天前我开始学习 powershell,但我在 google 上找不到任何可以满足我需要的东西,所以请耐心回答我的问题.

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.

我被要求将一些文本字符串替换为多个文件.我不一定知道可能的目标文件的扩展名,我也不知道它们的位置.到目前为止,我已经设法递归浏览到目录 (get-ChildItem -recurse) 并找到我正在寻找的字符串 get-content 和 select-string:

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"

问题是,我可以看到我正在寻找的文本的出现,但我不知道如何告诉 PS 返回每个匹配文件的路径和名称.

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:

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