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.

Cool Tip: Windows touch command equivalent in CMD and PowerShell! Read more →

Grep Command in Windows

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

# Windows CMD
C:\> netstat -na | findstr /c:"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 CMD
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 /r /c:"^SEARCH.*STRING$" file.txt

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

Options used by the findstr command in the example above:

Option Description
/i Case-insensitive search.
/c:"string" Use string as a literal search string. Without this option if the search string contains multiple words, separated with spaces, then findstr will return lines that contain either word (OR).
/r Evaluate as a regular expression.

Display help for the Windows grep command equivalents:

# Windows CMD
C:\> findstr /?

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

Cool Tip: Windows cat command equivalent in CMD and PowerShell! Read more →

Was it useful? Share this post with the world!

20 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!

    1. Fixed. 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

    1. use find instead of findstr (findstr will return all matches Directory or Service or Replication, find will return a match of the entire string (case sensitive). Use find -i to ignore case.

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

  5. auditpol.exe /get /category:* | findstr /C:”Directory Service Replication”

  6. Would you know why the Select-String command in PowerShell returns just the pattern match vs. findstr returning the entire line like grep would?
    For example:
    If I use Set-Alias -NAME grep -Value Select-String
    ‘Get-Alias |Select-String grep’ will return:
    grep
    ‘Get-Alias |findstr grep’ will return:
    Alias grep -> Select-String

    Is there a way to get the same output of Select-String sofor the sake of using PowerShell commands only?

    1. It is the behavior of the get-alias command that is giving the trouble.
      Turn the output into strings first:
      ‘Get-Alias | Out-String -stream | Select-String grep’

  7. Thanks a lot. useful for me.

  8. Nice information and may help me solve a mystery. thanks

    I’m trying to created a batch file for win 10 to find a particular process that likes to “hang” all the time. The thing is it’s pid changes every use. I’m an ex introductory level to intermediate programmer, and “hang” a lot of processes as I develop corny software in my old age. I need to use something like tasklist to list all NON RESPONSIVE processes, find the process or PID I need to kill and send that info to taskkill to get the job done. If anyone reads this and can help me out with a decent batch file, it would be very appreciated. I’ve read a lot of articles alreay, but can’t seem to get it to work. Thanks in advance to the best person I could honestly say at this point is the best person in the world 🙂

  9. I have a large TXT file with tens of thousands of lines.
    I want to find all the 500 or so lines that contain a particular string (for example “popup”) and extract those just lines into another output file for further processing. The string is unique to the lines I want extracted, and occurs just once in each such line.
    I have not programmed in years (fortran, BASIC, Excel macros), but I am willing to learn something modern. I am on a Win10 notebook and looking for advice: What would be the easiest & quickest (Python, Perl, Powershell, … ???) that would allow me to do the above?

    1. The powershell Select-String command will do that just fine with the right regex, as will most other shell languages. In powershell you can run this command:

      Select-string ‘^.*YOURSTRING.*’ c:\PATH\TO\INPUTFILE.TXT | %{$_.Matches.Value} > c:\PATH\TO\OUTPUTFILE.TXT

      Quick breakdown of that command:
      * The regex ^.*YOURSTRING.* selects the entirety of every line that contains YOURSTRING
      * %{$_.Matches.Value} selects only the regex lines; without it your output file will have the file name and line number on each line
      * > writes the output to a file

  10. for i in `ls lig*pdbqt`;do echo $i;grep ATOM $i | cut -c-66 > `basename
    $i .pdbqt`.pdb;done
    this is a linux command which i want to use on my windows cmd. Can someone convert it for me please? Thank you

  11. There is no reason why PowerShell and/or Command couldn’t implement “grep” and call it grep. Grep has been around since the start, thanks Ken Thompson, it’s open source and everyone knows it.

    Microsoft, don’t try to reinvent the wheel, it’s been around for a long time and it works.

    1. There’s not much reason for them to. Microsoft doesn’t have any reason to port GNU utilities that most Windows users won’t use. Those who do need them are going to be capable of installing it themselves. Installing the PCRE2 library for Perl through CPAN builds pcre2grep which is the faster JIT enabled full PCRE syntax grep as a native windows executable for example, or you could write a short perl script that implemented it using the builtin perl engine.

      Microsoft has a full regex engine that has more features than GNU egrep already but as soon as they made something *named* grep they’d be dealing with an endless stream of people wondering why some bizarro edge case copied from the GNU eregex implmentation returned different results. There’s no reason for them to do that to make a small group of people happy and get them hundreds of angy messages from linux users about trying to “steal marketshare” as though MS views linux as a competitor. When they introduced WSL2 the replies to the blog post were hilarious, just a bunch of linux people filled with hatred that microsoft was trying to keep people from installing linux on their machines…. by letting them install linux distros easily in Windows with more interop than a VM I guess? But don’t worry, I’m sure next year will be the year of Linux on the desktop and the year of Stahlman on a 7 year old finally.

  12. Very cool! Thank you!!!!

  13. If I had KNOWN this 30 years ago …

    Many Thanks 🙂

  14. Is there an option to find the running status of a windows service usnig the Select-String option ? Or is there any other option on powershell which could do that ?

Leave a Reply