Windows: `Grep` Equivalent – CMD & PowerShell

The grep command in Linux is widely used for parsing files and searching for useful data in the outputs of different commands.

The findstr command is a Windows grep equivalent in a Windows command-line prompt (CMD).

In a Windows PowerShell the alternative for grep is the Select-String command.

Below you will find some examples of how to “grep” in Windows using these alternatives.

Grep Command in Windows

Grep the output of a netstat command for a specific port:

# Windows CMD
C:\> netstat -na | findstr "PORT"

# Windows PowerShell
PS C:\> netstat -na | Select-String "PORT"

If a command in PowerShell returns some objects, before parsing, they should be converted to strings using the Out-String -Stream command:

# Windows PowerShell
PS C:\> Get-Alias | Out-String -Stream | Select-String "curl"

Grep a file for a pattern that matches a regular expression (case insensitive):

# Windows CMD
C:\> findstr /i "^SEARCH.*STRING$" file.txt

# Windows PowerShell
PS C:\> Select-String "^SEARCH.*STRING$" file.txt

Display help for the Windows grep command equivalents:

# Windows CMD
C:\> findstr /?

# Windows PowerShell
PS C:\> get-help Select-String

6 Replies to “Windows: `Grep` Equivalent – CMD & PowerShell”

  1. Hi. I tried to type in the cmd prompt
    ` C:\> netstat -na | findstr “2020”` and enter, and it doesn’t work. I want to search in directories of word document some strings. It seems that I missed something in your explanations

    1. What you want is this section of the article:
      # Windows CMD
      C:\> findstr /i “^SEARCH.*STRING$” file.txt
      In the example above you can change “^SEARCH.*STRING$” to “2020”. i.e.
      C:\> findstr /i “2020” *.txt
      The above command will search for “2020” in all txt files in your current directory.

  2. Great write-up. I found exactly what I needed.
    One correction, though, is in invoking findstr’s help, which follows the customary form of other Windows commands: findstr /?
    Thanks!

  3. PS C:\Users\Administrator> auditpol.exe /get /category:* | findstr “Directory Service Replication”
    Certification Services No Auditing
    Directory Service Changes No Auditing
    Directory Service Replication No Auditing
    Detailed Directory Service Replication No Auditing
    Directory Service Access No Auditing
    Kerberos Service Ticket Operations No Auditing
    Kerberos Authentication Service No Auditing

    i want exact match plz help me its urgent

  4. i want the one output that one exact match word to word

Leave a Reply