PowerShell Grep Equivalent

The grep command is widely used on Linux to parse files and shell output. Using grep you can easily find and filter the output returned by the previous command in the pipeline. In this article, we’ll take a look at the equivalents of the grep command in Windows PowerShell.

On the Windows Command Line (CMD), the equivalent to grep is findstr. However, it is difficult to use it in PowerShell scripts. The simplest PowerShell equivalent to grep is Select-String.

The Select-String cmdlet provides the following features:

  • Search by regular expressions (default);
  • Search by literal match (the parameter -Simple);
  • Search only the first match in the file, ignoring all subsequent ones (the –List switch);
  • Search for all matches, even if there are several of them in one line (-AllMatches);
  • Search for lines that don’t match the pattern (the –NotMatch parameter is similar to the –v switch of the grep tool);
  • In addition to the directly matched line, display several previous and next lines (the -Context argument);
  • Select-String allows specifying the encoding of the input file (parameter -Encoding). The following encodings are supported: ASCII, BigEndianUnicode, OEM, Unicode, UTF7, UTF8, UTF8BOM, UTF8NoBOM, UTF32.

For example, use the following command to find a list of open ports on your computer (with the Listening status):

netstat -na | Select-String "LISTENING"

powershell grep equivalent

If the previous pipe command returns objects rather than text, use the Out-String –Stream command before passing them to Select-String:

Get-Service | Out-String -Stream | Select-String "Running"

grep equivalent in powershell

You can use the regular expressions (RegEx) in the Select-String cmdlet. For example, the following command will find all files or lines containing the word “Error” in the specified folder:

Get-ChildItem -Path C:\PS | Select-String -Pattern 'Error'

If you need to check all sub-folders, use the -Recurse parameter:

Get-ChildItem "C:\PS\" -Filter *.log -Recurse | Select-String "Failed"|

To display the names of all found files and matching line numbers:

Get-ChildItem "C:\PS\" -Filter *.log -Recurse | Select-String "Failed"| Select Filename, LineNumber, Line, Path | Format-Table

grep powershell equivalent

You can copy all files with matches to the specified directory:

Get-ChildItem C:\logs\ -Filter *.log -Recurse | Select-String "Failed"| Copy-Item -Destination C:\Errors

As you can see, the Select-String cmdlet allows you to process string data. Use the Where-Object cmdlet if you need to grep objects (Filtering PowerShell objects using Where-Object).

For example, you want to find all processes that use more than 300 MB of RAM:

Get-Process | Where-Object { $_.WorkingSet -gt 314572800 }

windows grep equivalent

I enjoy technology and developing websites. Since 2012 I'm running a few of my own websites, and share useful content on gadgets, PC administration and website promotion.
Cyril Kardashevsky

One comment

  1. Thank you very much for the interesting contribution. Maybe you also have an answer to the following question:
    How can I search for an expression in one or more files with a command line and output this line as well as the two preceding and the three following with line number. (like with grep -B 2 -A3)
    I would be very happy about a solution ;-)
    Best regards

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.