1

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?

ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • Run the command twice? Once without the filter and once with. Maybe I'm not understanding what you're looking for. – Retired Ninja Feb 22 '18 at 14:23
  • @RetiredNinja sorry I am not following you, you want me to run the command twice and post the output? or your solution is to run the command twice? – ReynierPM Feb 22 '18 at 14:25
  • @ReynierPM Are you trying to get the line that matches + the line above it (containing the connection info)? – Mathias R. Jessen Feb 22 '18 at 14:26
  • It sounds like what you want is the complete output of the command followed by the filtered output of the command. In my mind it seems like running the command not piped through grep followed by the command piped through grep would get you what you need. `command && command | grep` Since `phpstorm64` doesn't occur in your unfiltered example output it's hard to follow what you really want. – Retired Ninja Feb 22 '18 at 14:31
  • @MathiasR.Jessen I have added the answer to you question to the OP to clarify it a little bit. – ReynierPM Feb 22 '18 at 14:34
  • @RetiredNinja I have added a version of the `netstat -abn` command showing `phpstorm64` being displayed. "It sounds like what you want is the complete output of the command followed by the filtered output of the command." no, this is not what I want, I want to display only the filtered output but with the whole `netstat` output, did you tried with some other program like chrome for example? run what I have tried and you'll see what I am talking about – ReynierPM Feb 22 '18 at 14:36
  • Sorry but it still doesn't make sense to me - if you want the full output, just run the command without `grep`/`findstr`/`Select-String`. If you want only the matching lines, do as you do now - if you want something else, please describe exactly what(, and perhaps why). "I want to display _only_ the filtered ouput but with the whole output" is like saying "I only like blue M&Ms, but I like all M&Ms" – Mathias R. Jessen Feb 22 '18 at 14:41
  • @MathiasR.Jessen is easy, I am running the command with the `findstr` is because the output of `netstat -abn` will show the full list of all the programs running and I need to manually look for the one I want, I would like to avoid that and list only that one I need to watch, is that so hard in PowerShell? this is a basic `grep` in Linux – ReynierPM Feb 22 '18 at 14:44
  • I don't have a linux box handy to try this on, but I believe the issue is that on Windows netstats is displaying the information as two lines and I'd guess linux displays it on one line. grep in a Windows bash prompt has the same issue you describe above. `netstat -abn | grep -A 1 "chrome"` seems to give the output you desire. I'm not sure what the equivalent option is for `findstr` to show an additional context line after the match. – Retired Ninja Feb 22 '18 at 14:50
  • @RetiredNinja So he _is_ looking for the matching line + the associated connection info (which is on the following line)? Am I missing something obvious here – Mathias R. Jessen Feb 22 '18 at 14:51
  • I don't understand the question either. @ReynierPM - please give an example of the output you are expecting. – Bill_Stewart Feb 22 '18 at 14:52
  • @MathiasR.Jessen I believe it's the matching line and the next line. I don't see an equivalent option in findstr to display next or previous context lines. – Retired Ninja Feb 22 '18 at 14:53
  • Give me a sec, adding more info and expected output :) – ReynierPM Feb 22 '18 at 14:53
  • 1
    I know you've found the answer, but your linux example uses a different command than your Windows example and what you're actually looking for. `netstat -an | findstr "TCP"` works exactly the same way on Windows. – Retired Ninja Feb 22 '18 at 15:14
  • @RetiredNinja perfect, thanks – ReynierPM Feb 22 '18 at 15:20

1 Answers1

3

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

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Perfect, this is exactly what I was looking for, didn't figured out what @RetiredNinja said about the two lines on the output. Thanks – ReynierPM Feb 22 '18 at 15:03