Search code examples
powershellwindows-10findstrnetstatselect-string

How do I get the full output of a command while using "findstr"?


I am trying to get the full output of the command netstat -abn while using Select-String or findstr but it's not working since only the search string is being listed. I am looking the same behavior as grep does on Linux where if you run netstat -an | grep tcp it will return the full output of netstat and will display all the info.

Here is an example in how the output from grep looks like in Linux:

$ netstat -an | grep tcp
tcp        0      0 0.0.0.0:44587               0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN
tcp        0     24 10.0.2.15:22                10.0.2.2:21724              ESTABLISHED
tcp        0      0 :::111                      :::*                        LISTEN
tcp        0      0 :::80                       :::*                        LISTEN
tcp        0      0 :::22                       :::*                        LISTEN
tcp        0      0 ::1:25                      :::*                        LISTEN
tcp        0      0 :::443                      :::*                        LISTEN
tcp        0      0 :::37224                    :::*                        LISTEN

When I am using findstr or Select-String just the name is being shown but the rest of the information is not there.

Here is an example of how the output looks like when using Select-String:

PS C:\windows\system32> netstat -abn | Select-String -Pattern "phpstorm64"

 [phpstorm64.exe]
 [phpstorm64.exe]
 [phpstorm64.exe]

Here is an example of how the output looks like when using findstr (using the alias I have created for it):

PS C:\windows\system32> New-Alias grep findstr
PS C:\windows\system32> netstat -abn | grep "phpstorm64"
 [phpstorm64.exe]
 [phpstorm64.exe]
 [phpstorm64.exe]

Here is an example of how the output looks like when you run the command netstat -abn alone without any findstr or Select-String:

PS C:\windows\system32> netstat -abn

Active Connections

  Proto  Local Address          Foreign Address        State
 [VBoxHeadless.exe]
  TCP    0.0.0.0:9001           0.0.0.0:0              LISTENING
 [phpstorm64.exe]
  TCP    0.0.0.0:10137          0.0.0.0:0              LISTENING
 [phpstorm64.exe]
  TCP    0.0.0.0:20080          0.0.0.0:0              LISTENING
 [phpstorm64.exe]
  TCP    0.0.0.0:33060          0.0.0.0:0              LISTENING
 [VBoxHeadless.exe]
  TCP    10.188.1.98:139        0.0.0.0:0              LISTENING

This is the output I want to achieve by using findstr or Select-String (I've just added one line but I would expect to see all the lines matching phpstorm64 word):

$ netstat -abn | grep "phpstorm64"
Proto  Local Address          Foreign Address        State
TCP    0 0.0.0.0:9001         0.0.0.0                LISTENING

Finally this is the version of PowerShell I am using:

PS C:\windows\system32> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14393  1944

How do I display the full output from the command before the pipe?


Solution

  • Use Select-String with the Context parameter. It allows you to include either preceding or following lines after a match:

    netstat -abn |Select-String -Pattern phpstorm64 -Context 0,1
    

    This will show you each matches line and the next line after it