PowerShell One Liner For Showing All Files In A Folder That Contain A String
Posted on September 29, 2019
- and tagged as
- powershell
Quick one today, this is probably the third or fourth time in the last few months I’ve needed to parse multiple files searching for a string, and each time I spend a little bit more time thinking about how to quickly do it than I’d like. So here it is for future reference, a quick one liner to print out the files that contain a string in a folder.
Get-ChildItem -File -Recurse -Force | % { if ((Get-Content $_.FullName) -match "SEARCH_STRING") {$_.FullName}}
This assumes there are only text files in the folder, I recommend adding filters to Get-ChildItem
if you have other file types too.
Note the -Force
argument, this is needed to catch hidden files. Get-ChildItem
has a -Hidden
option but this only shows hidden files whereas -Force
will show both.
Lastly, this should only be used for small files as PowerShell will read the entire file into memory, so beware.
Now I just need to remember that I wrote this next time I need it…