#PSTip How to speed up the Test-Connection command

Note: This tip requires PowerShell 2.0 and above

If you only like to know if a computer can be contacted across a network, you can use the Test-Connection cmdlet with the –Quiet parameter. You can even send only one echo request packet (ping) to the computer by using -Count 1.

This is much faster than executing Test-Connection without any parameters (which sends 4 echo requests by default), but if you have an unstable network connection or the computer is very busy, the ping reply can get lost!

To solve this situation, you can send more than one test which cost more time to wait for. To get the fastest reply possible, you can use a loop with a break or a function with a return statement, to get the earliest possible response. The delay time between every echo request in the Test-Connection cmdlet is set to the time interval in seconds. If you like to lower this interval you can use the Start-Sleep command.

Function Test-ConnectionQuietFast {

    [int]$Count = 1,
    [int]$Delay = 500

    for($I = 1; $I -lt $Count + 1 ; $i++)
        Write-Verbose "Ping Computer: $ComputerName, $I try with delay $Delay milliseconds"

        # Test the connection quiet to the computer with one ping
        If (Test-Connection -ComputerName $ComputerName -Quiet -Count 1)
            # Computer can be contacted, return $True
            Write-Verbose "Computer: $ComputerName is alive! With $I ping tries and a delay of $Delay milliseconds"
            return $True

        # delay between each pings
        Start-Sleep -Milliseconds $Delay

    # Computer cannot be contacted, return $False
    Write-Verbose "Computer: $ComputerName cannot be contacted! With $Count ping tries and a delay of $Delay milliseconds"

# Example:
Test-ConnectionQuietFast -ComputerName localhost -Count 3 -Delay 300 –Verbose

About the author: Peter Kriegel

Working with Computers Since 1984. Programming Computers Since 1990 (C,C++ and Basic Languages). Computer Administrator since 2000 (MCSA 2000). Using VBScript,DOS Batches, Office VBA. Programming with VB.NET since 2005 (.NET V2) against MS SQL Server Databases (GUI Frontends). Using and Scripting PowerShell since 2009. Feel the Power! ;-) I am a OU and System-Administrator in a very Large Active Directory Domain (> 100000 Workstations). I am Administering 3000 Windows Workstations in 37 OUs in a small Team.

Related Posts