Powershell equivalent of `grep -r -l` (--files-with-matches)
You can use Select-String
to search for text inside files, and Select-Object
to return specific properties for each match. Something like this:
Get-ChildItem -Recurse *.* | Select-String -Pattern "foobar" | Select-Object -Unique Path
Or a shorter version, using aliases:
dir -recurse *.* | sls -pattern "foobar" | select -unique path
If you want just the filenames, not full paths, replace Path
with Filename
.
Explanation:
Get-ChildItem
-Recurse *.*
returns all files in the current directory and all its subdirectories.Select-String
-Pattern "foobar"
searches those files for the given pattern "foobar".Select-Object
-Unique Path
returns only the file path for each match; the-Unique
parameter eliminates duplicates.
Note, that in powershell v1.0 and v2.0 you need to specify first position parameter (path) to work with -Recursion
technet documentation
-Recurse
Gets the items in the specified locations and in all child items of the locations.
In Windows PowerShell 2.0 and earlier versions of Windows PowerShell, the Recurse parameter works only when the value of the Path parameter is a container that has child items, such as C:\Windows or C:\Windows*, and not when it is an item does not have child items, such as C:\Windows*.exe.