Search code examples
powershellnetstat

How can I show the netstat command in powershell without the 0 in the Local address?


I hope I could explain, sorry for my english

  Proto  Local Address          Foreign Address        State           PID
  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING       1160
  TCP    0.0.0.0:445            0.0.0.0:0              LISTENING       4
  TCP    0.0.0.0:5040           0.0.0.0:0              LISTENING       8864
  TCP    0.0.0.0:5357           0.0.0.0:0              LISTENING       4
  TCP    0.0.0.0:7680           0.0.0.0:0              LISTENING       14052
  TCP    0.0.0.0:49664          0.0.0.0:0              LISTENING       964
  TCP    0.0.0.0:49665          0.0.0.0:0              LISTENING       872
  TCP    0.0.0.0:49666          0.0.0.0:0              LISTENING       1696
  TCP    0.0.0.0:49667          0.0.0.0:0              LISTENING       1448
  TCP    0.0.0.0:49668          0.0.0.0:0              LISTENING       3380
  TCP    0.0.0.0:49710          0.0.0.0:0              LISTENING       944

but what i want

Local Address
135
445
5040
5357
7680
49664
49665
49666
49667
49668
49710

Also, how can I show this on the screen with what code?


Solution

  • If the string output is acceptable, then one of the easiest ways to achieve your desired result is to simply remove the unwanted string with regex. However it will mess up the formatting.

    (netstat -ano) -replace '0\.0\.0\.0:'
    
      Proto  Local Address          Foreign Address        State           PID
      TCP    135            0              LISTENING       868
      TCP    445            0              LISTENING       4
      TCP    5040           0              LISTENING       7288
      TCP    5357           0              LISTENING       4
      TCP    5985           0              LISTENING       4
      TCP    6783           0              LISTENING       5128
      TCP    47001          0              LISTENING       4
      TCP    49664          0              LISTENING       976
      TCP    127.0.0.1:6463         0              LISTENING       14660
      TCP    127.0.0.1:6800         0              LISTENING       7468
      TCP    127.0.0.1:8094         0              LISTENING       4348
    

    This is a huge drawback from Powershell's object based output. You could try to correct the alignment manually if you so desire..

    (netstat -ano) -replace '0\.0\.0\.0:(\d+)','$1        '
    
      Proto  Local Address          Foreign Address        State           PID
      TCP    135                    0                      LISTENING       868
      TCP    445                    0                      LISTENING       4
      TCP    5040                   0                      LISTENING       7288
      TCP    5357                   0                      LISTENING       4
      TCP    5985                   0                      LISTENING       4
      TCP    6783                   0                      LISTENING       5128
      TCP    47001                  0                      LISTENING       4
      TCP    127.0.0.1:8094         0                      LISTENING       4348
      TCP    127.0.0.1:8763         0                      LISTENING       5128
      TCP    127.0.0.1:9527         0                      LISTENING       5128
      TCP    127.0.0.1:37014        0                      LISTENING       4576
    

    Again, these examples really only benefit the user viewing it. If you want to use the data later on, you'd have to parse it. At this point you really should look at the powershell alternatives such as Cpt.Whale's answer shows.