In Windows 8.1, the Test-NetConnection cmdlet is useful for checking the state of a network port on a remote system. However, it can be unnecessarily slow sometimes. I'd like to know if there's some options I could tweak, or an alternative PowerShell command I could use, to make this process faster.

Test-NetConnection can take around 10 seconds to return results if a remote system is not responding. Whenever a port is specified, it runs two connection tests which take about 5 seconds each to timeout. The first test is a basic ICMP echo check. This will timeout if the system is offline, or if it (or any intervening infrastructure) is configured to block or not respond to ICMP echo requests.The second test is the actual check against the specified port. This will timeout if the system is offline, or if there is a firewall along the path that is blocking the port.

In my current use case, the remote system is only two hops away on a reliable Gigabit Ethernet connection. So, a five-second timeout for any request is quite excessive - I could probably still get reliable results with a timeout of 30 ms or less! Additionally, the system is known to be non-responsive to ICMP echo even though it may be online and have all other services available. So it would be great if I could do without the ICMP echo test entirely, and reduce the timeout for the TCP connection test, in order to speed up my scripts that use Test-NetConnection for this purpose.

Does Test-NetConnection have options to change these behaviors? (I've read the detailed help file, and the answer seems to be no - but I'd be happy to be told there's something I've missed.) Or is there another way I can use PowerShell to run the same checks, but faster?

For various reasons, I prefer to keep my scripts limited to using functionality built-in to the Operating System wherever possible. Presume the environment is a fresh build of Windows 8.1, with all appropriate Windows Updates applied, and third-party tools are not an option.


You can use this to test the connection - Taken from the PowerShell Code Repository (author 'BSonPosh'):

"Test-Port creates a TCP connection to specified port. By default it connects to port 135 with a timeout of 3secs."


# Test-Port.ps1
# Does a TCP connection on specified port (135 by default)

$ErrorActionPreference = "SilentlyContinue"

# Create TCP Client
$tcpclient = new-Object system.Net.Sockets.TcpClient

# Tell TCP Client to connect to machine on Port
$iar = $tcpclient.BeginConnect($srv,$port,$null,$null)

# Set the wait time
$wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false)

# Check to see if the connection is done
    # Close the connection and report timeout
    if($verbose){Write-Host "Connection Timeout"}
    Return $false
    # Close the connection and report the error if there is one
    $tcpclient.EndConnect($iar) | out-Null
    if(!$?){if($verbose){write-host $error[0]};$failed = $true}

# Return $true if connection Establish else $False
if($failed){return $false}else{return $true}

You can go to that repository page for follow-ups (this answer is already too much of a copy-job)


Very basic (timeout 100 ms):

function testport ($hostname='yahoo.com',$port=80,$timeout=100) {
  $requestCallback = $state = $null
  $client = New-Object System.Net.Sockets.TcpClient
  $beginConnect = $client.BeginConnect($hostname,$port,$requestCallback,$state)
  Start-Sleep -milli $timeOut
  if ($client.Connected) { $open = $true } else { $open = $false }


hostname  port  open
--------  ----  ----
yahoo.com   80  True

An even quicker way could be :

New-Object System.Net.Sockets.TCPClient -ArgumentList $ip, $port

The result would be :

Client              : System.Net.Sockets.Socket
Available           : 0
Connected           : True
ExclusiveAddressUse : False
ReceiveBufferSize   : 65536
SendBufferSize      : 65536
ReceiveTimeout      : 0
SendTimeout         : 0
LingerState         : System.Net.Sockets.LingerOption
NoDelay             : False

The interessting value is "Connected"

edit : one more reason : Test-NetConnection only works from Powershell v5 (if I remember correctly), while this solution works from v2 :)

  • This way takes much longer to time out if the host is unreacheable than Jan's solution. – Mark Allison Nov 22 '17 at 17:12
  • Works on PS v4.0 too – Janis Veinbergs Apr 13 '18 at 12:43

I had been searching for a super fast way ping many IPs and stumbled across this question (among others).

Eventually, I found a script that was easy to integrate into what I wanted to do. The guy calls it Fast Ping Sweep Asynchronous.

Even being a Power Shell n00b, I was able to pipe its output, and then modify its output to only include what I wanted. I came across other scripts, but could not decipher their scripts to modify them for my purposes.

I'm not sure what version Power Shell this requires, but it works on v4 and v5.

I have seen most of the Powershell IP scanner, ping sweeping scripts but none of them uses the PingASync method.The "problem" with synchronous scripts is that they have to wait until a node replies or times out before continuing to the next address.Using this approach may take s

function Global:Ping-IPRange {
        Sends ICMP echo request packets to a range of IPv4 addresses between two given addresses.

        This function lets you sends ICMP echo request packets ("pings") to 
        a range of IPv4 addresses using an asynchronous method.

        Therefore this technique is very fast but comes with a warning.
        Ping sweeping a large subnet or network with many swithes may result in 
        a peak of broadcast traffic.
        Use the -Interval parameter to adjust the time between each ping request.
        For example, an interval of 60 milliseconds is suitable for wireless networks.
        The RawOutput parameter switches the output to an unformated

        You cannot pipe input to this funcion.

        The function only returns output from successful pings.

        Type: System.Net.NetworkInformation.PingReply

        The RawOutput parameter switches the output to an unformated

        Author  : G.A.F.F. Jakobs
        Created : August 30, 2014
        Version : 6

        Ping-IPRange -StartAddress -EndAddress -Interval 20

        IPAddress                                 Bytes                     Ttl           ResponseTime
        ---------                                 -----                     ---           ------------                                 32                      64                    371                                 32                     128                      0                                 32                     128                      1                                 32                      64                     88                                32                      64                      0

        In this example all the ip addresses between and are pinged using 
        a 20 millisecond interval between each request.
        All the addresses that reply the ping request are listed.


        [parameter(Mandatory = $true, Position = 0)]
        [parameter(Mandatory = $true, Position = 1)]
        [int]$Interval = 30,
        [Switch]$RawOutput = $false

    $timeout = 2000

    function New-Range ($start, $end) {

        [byte[]]$BySt = $start.GetAddressBytes()
        [byte[]]$ByEn = $end.GetAddressBytes()
        $i1 = [System.BitConverter]::ToUInt32($BySt,0)
        $i2 = [System.BitConverter]::ToUInt32($ByEn,0)
        for($x = $i1;$x -le $i2;$x++){
            $ip = ([System.Net.IPAddress]$x).GetAddressBytes()
            [System.Net.IPAddress]::Parse($($ip -join '.'))

    $IPrange = New-Range $StartAddress $EndAddress

    $IpTotal = $IPrange.Count

    Get-Event -SourceIdentifier "ID-Ping*" | Remove-Event
    Get-EventSubscriber -SourceIdentifier "ID-Ping*" | Unregister-Event

    $IPrange | foreach{

        [string]$VarName = "Ping_" + $_.Address

        New-Variable -Name $VarName -Value (New-Object System.Net.NetworkInformation.Ping)

        Register-ObjectEvent -InputObject (Get-Variable $VarName -ValueOnly) -EventName PingCompleted -SourceIdentifier "ID-$VarName"

        (Get-Variable $VarName -ValueOnly).SendAsync($_,$timeout,$VarName)

        Remove-Variable $VarName


            $pending = (Get-Event -SourceIdentifier "ID-Ping*").Count

        }catch [System.InvalidOperationException]{}

        $index = [array]::indexof($IPrange,$_)

        Write-Progress -Activity "Sending ping to" -Id 1 -status $_.IPAddressToString -PercentComplete (($index / $IpTotal)  * 100)

        Write-Progress -Activity "ICMP requests pending" -Id 2 -ParentId 1 -Status ($index - $pending) -PercentComplete (($index - $pending)/$IpTotal * 100)

        Start-Sleep -Milliseconds $Interval

    Write-Progress -Activity "Done sending ping requests" -Id 1 -Status 'Waiting' -PercentComplete 100 

    While($pending -lt $IpTotal){

        Wait-Event -SourceIdentifier "ID-Ping*" | Out-Null

        Start-Sleep -Milliseconds 10

        $pending = (Get-Event -SourceIdentifier "ID-Ping*").Count

        Write-Progress -Activity "ICMP requests pending" -Id 2 -ParentId 1 -Status ($IpTotal - $pending) -PercentComplete (($IpTotal - $pending)/$IpTotal * 100)


        $Reply = Get-Event -SourceIdentifier "ID-Ping*" | ForEach { 
            If($_.SourceEventArgs.Reply.Status -eq "Success"){
            Unregister-Event $_.SourceIdentifier
            Remove-Event $_.SourceIdentifier


        $Reply = Get-Event -SourceIdentifier "ID-Ping*" | ForEach { 
            If($_.SourceEventArgs.Reply.Status -eq "Success"){
                $_.SourceEventArgs.Reply | select @{
                      Name="IPAddress"   ; Expression={$_.Address}},
                    @{Name="Bytes"       ; Expression={$_.Buffer.Length}},
                    @{Name="Ttl"         ; Expression={$_.Options.Ttl}},
                    @{Name="ResponseTime"; Expression={$_.RoundtripTime}}
            Unregister-Event $_.SourceIdentifier
            Remove-Event $_.SourceIdentifier
    if($Reply -eq $Null){
        Write-Verbose "Ping-IPrange : No ip address responded" -Verbose

    return $Reply

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.