Windows Findstr Command Examples

Findstr is a text manipulation tool similar to Linux grep command. This CMD command can be quite useful at times to filter text and output of other commands.

In this tutorial, we will go through several examples to learn the findstr command.

  • The findstr command is used to search text strings in files or standard input.
  • It can search files recursively on a folder and all sub-folders.
  • The most common use of findstr command is to filter the output of other commands.
  • This command works on both CMD and PowerShell.
  • Findstr is case-sensitive by default. To do case-insensitive searches, we use the /I option.

Basic Search

In its basic form, findstr searches for the given string(s) on one or more files.

findstr string file_name

For example, let's say we have a file named data.txt with the following text inside:

windows 10
Windows 10
windows 11
Windows 11
Ubuntu
Mac

The command findstr "win" data.txt displays the following output:

findstr windows command
findstr command

It returns lines that contain win. Because findstr search is case sensitive, it only matched windows 10 but not Windows 10. You can use the /I option if you want search is not to be case-sensitive, as shown in the following example:

findstr /i "win" data.txt

The /I is the one option you will use more often than not.

findstr case-sensitive search
Case-sensitive Search

Another useful command option is /N to print the line number before each line that matches.

findstr display line numbers
Display line numbers

Literal Search (Exact Match)

If you separate words with spaces, findstr will treat each word as a separate string. For example, findstr /i "windows 10" data.txt will return lines that contain either Windows or 10.

Words with spaces

To avoid that, use the /c: option as shown in the following example:

findstr /i /c:"windows 10" data.txt

This time findstr treats windows 10 as a single string and will only return the lines containing the string windows 10.

Findstr Literal Search
Literal Search

Using Findstr to Filter the Output of Another Command

More often than not, you will use findstr to filter the output of another command.

Following is an example:

tasklist | findstr /i svchost

In the above example, you can see that I piped the output of the tasklist command to findstr and then told findstr to filter only text that contained the keyword svchost.

tasklist findstr
tasklist command

Here is another example in which I piped the output of the systeminfo command to findstr to check the Windows version:

systeminfo | findstr /i  /c:"os name"

Lines That Do Not Match

If you use the /v option, it will print all lines that do not include the string you provided.

findstr /i /v "win" data.txt

The above command will print lines that do not contain win.

Lines That Do Not Match

Recursive Search

If you are familiar with the grep command, you know one standout feature is the ability to search all files in a directory and all subdirectories.

We can also do it with findstr using the /S option, as shown in the following example:

findstr /i /s "windows" data.txt

In the above example, findstr will look for the data.txt on the current directory and all subdirectories.

Use * in place of the filename to search every file in the current working directory, as shown in the following example:

findstr /i /s "windows" *

Here is another example in which we search all files in C:\data\ and all subdirectories:

findstr /i /s "windows" C:\data\*

Search Multiple Files

We can search for multiple files at once, as shown in the following example:

findstr /i "windows" data.txt data2.txt

In the following example, we search the string windows on every file that ends with the .txt extension:

findstr /i "windows" *.txt

Search on files whose names begin with data:

findstr /i "windows" data*

Positional Searching

For positional searching, findstr have /B and /E options to match patterns at the beginning and the end of a line.

findstr /i /b "ms" data.txt

The above command returns the line that begins with ms. The following command returns the lines ending with 10.

findstr /i /e "10" data.txt

Wildcards

The findstr command supports wildcards (regular expressions) for advanced text searches. The most common wildcard is the asterisk (*), which is used to match any number of characters.

Examples

Search for the string win followed by any number of characters:

findstr /i win* data.txt

Search for words starting with "win" followed by any number of characters:

findstr /i \<win. data.txt

Search for the words that end with "nux":

findstr /i "nux\>" data.txt

Search for the word that starts with any character(s) and ends with "nux":

findstr /i ".nux\>" data.txt

Search for the string Windows in any file that ends with .txt extension in C:\data\ and all subdirectories:

findstr /i /s "windows" C:\data\*.txt

Search for word Windows in any files that end with log in the C:\data\ and all subdirectories:

findstr /i /s "windows" C:\data\*log

For a list of all the options, look at the command help by typing findstr /?.