Share this post:This blog post is part of an ongoing series by Adam Gordon. Adam will show you how to use each PowerShell command each week. Adam will be covering Test-Connection this week.
When should you use Test-Connection
The Test-Connection cmdlet sends Internet Control Message Protocol echo request packets (or pings) to one or more remote computers, and returns the echo reply replies. This cmdlet can be used to determine if a computer can be reached across an IP network.
To specify the sending and receiving computers as well as the timeout and number of pings and to configure authentication and connection, you can use Test-Connection’s parameters.
Unlike the familiar ping command, Test-Connection returns a TestConnectionCommand+PingStatus object that you can investigate in PowerShell.
The -Quiet parameter returns the Boolean value of a System.Boolean object per tested connection. An array of Boolean value is returned if multiple connections are being tested.
What version of PowerShell should I use for this blog?
Get the PowerShell Version for your machine
$PSVersionTable
This command displays the PowerShell version information for your machine.
How to use TestConnection
Echo requests sent to remote computers
Test-Connection-TargetName SCCMDC-IPv4
Test-Connection uses -TargetName to specify the SCCMDC computer. The protocol for the test is specified by the -IPv4 parameter.
A series of TestConnectionCommand+PingStatus objects are sent to the output stream, one object per ping reply from the target machine.
You can customize the test command by using parameters
Test-Connection -TargetName SCCMDC -Count 3 -Delay 2 -MaxHops 128 -BufferSize 256
Test-Connection uses -TargetName to specify SCCMDC. The -Count parameter specifies that three pings are sent by Test-Connection to the SCCMDC computer at 2-second intervals.
NOTE: These options may be used when the ping response is expected take longer than usual due to a long hop count or a high-traffic network condition.
As a background job, take a test:
Line # 1: $job = Start-Job -ScriptBlock Test-Connection -TargetName (Get-Content -Path “C:\Test\Servers.txt”)
Line #2: $Results = Receive Job $job -Wait
The Test-Connection cmdlet is used to ping computers by the Start-Job command. The -TargetName parameter’s value is a Get Content command that reads the Servers.txt file for a list computer names.
This command uses the StartJob cmdlet for background jobs and saves the job to the $job variable.
The Receive-Job command instructs the receiver to wait until the job is complete. After that, the receiver gets the results and stores them into the $Results variable.
Only create a session if the connection test passes:
if (Test-Connection -TargetName SCCMDC -Quiet) New-PSSession -ComputerName SCCMDC
The Test-Connection cmdlet sends a ping to the SCCMDC computer. It uses the -Quiet parameter. If any of the four pings succeed, the result is $True. If none of the four pings succeed, then the value is $False.
If Test-Connection returns $True, the command uses New-PSSession cmdlet. This creates the PSSession.
Use Traceroute
Test-Connection -TargetName www.google.com -Traceroute
Introduced in PowerShell 6.0, the –Traceroute parameter maps a route from the local computer to the remote destination that you specify using the –TargetName parameter.
Learn last week’s command: Get-AuthenticodeSignature.
Do you need PowerShell training? ITProTV offers PowerShell online IT training courses.