find and findstr: grep for Windows

2 min read

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    0.0.0.0:135            0.0.0.0:0              LISTENING       816
  TCP    0.0.0.0:445            0.0.0.0:0              LISTENING       4
  TCP    0.0.0.0:902            0.0.0.0:0              LISTENING       2892
(...)

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

C:\>find "hello" *

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

---------- 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
24

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" *
test1.txt:hello
test2.txt: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