Question

I use findstr to recursively search several large directories for any lines that match any of the lines in searchList.txt.

findstr /SNIP /D:"C:\search Dir1;C:\search Dir2" /G:C:\searchList.txt *

I want to do the same thing in a Powershell script, either by calling the findstr command or doing something entirely in Powershell. Iterating over each item in searchList.txt and then looking in all the directories is too slow.

How do I call the above line in Powershell, or do the equivalent?

Thanks in advance.

Was it helpful?

Solution

You can call it in PowerShell by opening a PowerShell prompt, typing it in, and hitting [ENTER].

I don't see anything preventing you from running that command from PowerShell. findstr is not a native cmd command, it's a standalone .exe file bundled with Windows and located in the default system path (the System32 directory), so it's available from PowerShell.

You could do it in native PowerShell, but that would be an unnecessary headache. You'd have to do something like this

echo 'C:\search Dir1' 'C:\search Dir2' | Get-ChildItem | Select-String ((Get-Content C:\searchlist.txt) -join '|')

but excluding binary files would be a pain, because AFAIK there's no built-in way to check whether a file is text or binary. If you can rely on the extension, you could add -Filter *.txt after Get-ChildItem.

Bottom line: It's not worth it. Select-String is more versatile in terms of matching patterns, because you can use .NET regular expressions, but if the patterns you want to find are within findstr's limited regex capabilities, you're better off using findstr for this task, and your scripts will be portable because it's always included in Windows.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top