, , ,

PowerShell: Ping IP range

1–2 minutes
15 comments on PowerShell: Ping IP range

Powershell

Some you need to know which IP in a specific range is already in use. With Windows PowerShell there is a simple way to ping a IP range.

You can use the .Net class System.Net.Networkinformation.Ping to do this.


$ping = New-Object System.Net.Networkinformation.Ping
1..254 | % { $ping.send(“192.168.100.$_”) | select address, status }

You can also have a look at the new Test-Connection or Test-NetConnection PowerShell cmdlets.

15 responses

  1. Jeffery Hicks (MVP) Avatar

    First, if you want a bit more detail or more control over the ping, you could use the Test-Connection cmdlet. If you want a simple up/down you could use something like this:

    1..254 | foreach { new-object psobject -prop @{Address=”172.16.10.$_”;Pin
    g=(test-connection “172.16.10.$_” -quiet -count 1)}}

    I’m only sending a single ping in this example.

    The other problem with the ping class is that if the destination is unreachable, the address you get back is yours., I don’t think that is what you intended.

  2. Jeffery Hicks (MVP) Avatar

    I took this and ran with it.
    Ping IP Range with Windows #PowerShell http://bit.ly/sCjeCR

  3. mahmood Avatar

    thanks. it was very usefull

  4. mahmood Avatar

    It is possible to filter unreachable hosts and shows only “success” hosts?

  5. Lars Panzerbjørn Avatar
    Lars Panzerbjørn

    I know I’m a bit late to the party, but I thought I’d add that when I run the commands, only xxx.xxx.xxx.1 is pinged…
    But if I run “1..254 | % { write-host $_ }” then it gives me the number 1 to 254, each on a separate line…

  6. Lars Panzerbjørn Avatar
    Lars Panzerbjørn

    Hah, I figured it out, of course it only returns my machines own address until it finds an address that actually return a ping -_-

  7. Stefan Stranger Avatar

    Hi Thomas,

    I created a ping ip range script using PowerShell workflows :-)

    Check my github: https://github.com/stefanstranger/PowerShell/commit/61e676459e7eed70e811aafd3e03b83c37668821

    /Stefan

  8. Pablo from Argentina Avatar

    Hi all , i need to query specific segments and get the segment and description from sites and services

    199.52.213.0/24
    199.52.241.128/26
    199.49.54.0/23 (Segment not created)
    199.49.63.0/26 (Segment not created)
    199.49.65.0/26 (Segment not created)
    199.49.65.64/26 (Segment not created)
    199.49.70.0/26 (Segment not created)

    and os on

    is there a powerhsle to export all IP Segments to CSV file ?

  9. Mikael Hansen Avatar

    I know it’s old but if someone stumbles upon this and has the same question as Mahmood:

    Change the second line to this:
    1..254 | % { $ping.send(“192.168.1.$_”) | where {$_.status -eq “Success”} | select address, status }

  10. Niklas Avatar

    Hi. I get this output, (address column truncated).
    Any easy way to fix that?

    Address Status
    150…. Success
    Timedout
    150…. Success

  11. BM Avatar

    1..254 | foreach { new-object psobject -prop @{Address=”172.16.10.$_”;Pin
    g=(test-connection “172.16.10.$_” -quiet -count 1)} | Format-Table -autosize}

  12. IdaM Avatar

    This may work, but has horrible readability. I would never use a command like this in my code. I would prefer Stefan Strangers solution.

  13. Daniel Butler Avatar
    Daniel Butler

    Thank for putting this up there, nice quick method to test a load of IPs :)

Leave a Reply

Your email address will not be published. Required fields are marked *

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