Test-Connection with Timeout

by Jan 14, 2016

The Test-Connection cmdlet implements a simple ping to check whether a system responds to an ICMP request. Unfortunately, you cannot specify a timeout. Test-Connection defaults to a static timeout of 4 seconds:

 
PS C:\> Test-Connection -ComputerName powershellmagazine.com -Count 1 | Select-Object Address, ResponseTime, Timeout


Address                ResponseTime Timeout
-------                ------------ -------
powershellmagazine.com          118    4000 
 

To use a timeout, simply use the underlying WMI query like this:

$ComputerName = 'powershellmagazine.com'
$Count = 1
$Timeout = 1000
$Filter = 'Address="{0}" and Timeout={1}' -f $ComputerName, $Timeout
Get-WmiObject -Class Win32_PingStatus -Filter $Filter |
  Select-Object Address, ResponseTime, Timeout

Here is the result:

 
Address                ResponseTime Timeout
-------                ------------ -------
powershellmagazine.com          118    1000 
 

To ping multiple times, simply use a loop.

Throughout this month, we'd like to point you to two awesome community-driven global PowerShell events taking place this year:

Europe: April 20-22: 3-day PowerShell Conference EU in Hannover, Germany, with more than 30+ speakers including Jeffrey Snover and Bruce Payette, and 60+ sessions (www.psconf.eu).

Asia: October 21-22: 2-day PowerShell Conference Asia in Singapore. Watch latest annoncements at www.psconf.asia

Both events have limited seats available so you may want to register early.

Twitter This Tip! ReTweet this Tip!