Findstr: Find Specific String In Files With Windows Command (Grep Alternative)

Most of the users who use Linux are very comfortable with using command-line. And one of the basic requirement is to find specific strings within specific file or files or even from the output of a particular command. Grep is a command-line option used to find a specific string from inside a file or multiple files or from an output of a command but it can be used only in Linux. For Windows, the grep alternative is findstr.

Since Windows users are not used to use command-line for smaller things, most of the users don’t know how to find a specific string in files using Windows command-line or even PowerShell. In this article, we will discuss about how to use findstr (equivalent of Grep in Windows) in command prompt and also how to use the find function using PowerShell. Let’s discuss about different scenarios which can be used in real-world situations.

Filter an output of a command Using Command Prompt

If you want to filter the results of a command, you can use | findstr “string_to_find”

For example, I mostly use netstat for checking the connections being made on my computer. If I want to check which app or IP address is connected to a specific port, I’ll use the following command:

netstat | findstr “imaps”

Findstr filtering imaps ports
Findstr filtering imaps ports

This will show me only secure imap ports opened on my computer.

Search for a specific string inside a single file Using Command Prompt

The command for this purpose is:

findstr “string_to_find” “file_name”

For example,

findstr “reader” “new 1.txt”

You can also give full path of the file if it’s not in the same directory as opened in command prompt.

find text string in a file
find text string in a file

Search for a specific string in a folder using Findstr

You can also specify a folder for finding a specific text string in multiple files.

findstr /M “reader” “C:\Users\Usman\Desktop\*”

This will give a list of all files with full path containing the text string “reader”. If you don’t specify /M, the output will show the exact text string along with the file name where it found the string.

Finding a string from folder
Finding a string from folder

You can go through all the switches you can use with the command here.

This command can be useful in many cases especially when I am creating a log of network activities and have to find a specific thing from the log. What do you do with this filter command “findstr”?

Join 6000+ other users and have new posts emailed to you

Discover new tech tips and troubleshooting guides as soon as they are published

7 thoughts on “Findstr: Find Specific String In Files With Windows Command (Grep Alternative)”

  1. The author seems to have forgotten the article was also to discuss “how to use the find function using PowerShell”.

    Reply
  2. Thanks for giving the grep equivalent in windows. I wish you come up with equivalents of all commonly used unix commands and compile in a file so that users like me can use it as a reference book. Thanks once again.

    Padmanabhan

    Reply
  3. I’ve been trying to find filenames that start with a certain string.
    I started by finding all files within a certain directory which contains a bunch of sub directories and such by ‘dir * /s/b | findstr “.*\..*”‘ I pipped the results to a DirectoryListing.txt file to store all the paths.
    I also have a List.txt file which contains strings of filenames, ie: ‘12345’. Each filename is on its own line.
    I then tried to find all filenames that start with strings contained within the List.txt from my DirectoryListing.txt file. In doing so I got filenames that didn’t start with my searched string. If I took my string ‘12345’, I would expect to get back filenames that start with ‘12345’, but I also received filenames that included a hyphen ’11-12345′. Like in your example where you FINDSTR ‘reader’ it returned “adobe-reader-…”.
    This is how I am trying to parse the file: For /F “Delims=|” %%X In (‘FINDSTR /IRC:”\” “%destination%\%DirList%”‘)
    %%A is the string to find, ie: ‘12345’
    %DirList% is my DirectoryListing.txt

    My question is how would I be able to use FINDSTR to find filenames that start with a certain string and not have a ‘-‘ hyphen before the string?

    Reply
  4. Do I understand correctly: findstr matches a STRING, not a regular expression pattern, right?

    And you call that an “equivalent to grep”??

    Reply
  5. I am trying to filter a gitbranch name so I used git branch | findstr “*” it is outputting a branch name but including the * sign. I was wondering if there is any way to select the exact name of the branch and save it to the string.
    SPecially for branches lik rel-0.1 it is not working.

    I am trying to convert git branch | grep \* | cut -d ‘ ‘ -f2 | git pull origin from bash script to
    >git branch | findstr “*” | git pull origin but anytime the branch name is like rel-0.1 it does not work but for others it is working.
    The goal is to pull origin from the current selected branch.

    Reply

Leave a Comment