find and findstr: grep for Windows

On a Windows server there's also a way of fitering other command's output just like you would do using grep on a Linux: You can do it using find and findstr

We can filter the output of another command like this:

C:\>netstat -ano | find "LISTEN"
  TCP                LISTENING       816
  TCP                LISTENING       4
  TCP                LISTENING       2892

But we can also ise it to find a string inside files:

C:\>find "hello" *

---------- TEST1.TXT

---------- TEST2.TXT

---------- TEST3.TXT
hello world

It has some options quite similar to grep:

  • /v: Invert, show the lines not containing the string
  • /i: To ignore case
  • /c: Count the number of times there's a given string:
C:>netstat -ano | find "LISTEN" /c

With findstr we can also use regular expresions:

C:\>tasklist | findstr "[cC]onsole"
winlogon.exe                   892 Console                    1     7.388 KB

If we try to search for a string containing spaces, it won't work straight away since it's going to search for the words separately:

C:\Users\Jay\Desktop\test>findstr "hello world" *
test3.txt:hello world

To be able to search for the entire string we will have to use the option /c like so:

C:\>findstr /c:"hello world" *
test3.txt:hello world

Posted on 09/04/2021