0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Test Network Connectivity Quickly

Use the PowerShell command Test-Connection -ComputerName 'hostname_or_ip' -Count 2 to test network connectivity by sending ping requests.
📋

Examples

InputTest-Connection -ComputerName '8.8.8.8' -Count 2
OutputSource Destination IPV4Address IPV6Address Bytes Time(ms) TTL ------ ----------- ----------- ----------- ----- -------- --- YourPCName 8.8.8.8 8.8.8.8 32 20 117 YourPCName 8.8.8.8 8.8.8.8 32 21 117
InputTest-Connection -ComputerName 'localhost' -Count 1
OutputSource Destination IPV4Address IPV6Address Bytes Time(ms) TTL ------ ----------- ----------- ----------- ----- -------- --- YourPCName localhost 127.0.0.1 32 0 128
InputTest-Connection -ComputerName '192.0.2.123' -Count 1
OutputTest-Connection : Ping request could not find host 192.0.2.123. Please check the name and try again.
🧠

How to Think About It

To test network connectivity, you send a small message called a ping to the target computer or IP address. If the target replies, it means the network path is working. PowerShell's Test-Connection command does this by sending ICMP echo requests and waiting for replies.
📐

Algorithm

1
Get the target hostname or IP address as input.
2
Send a ping request to the target using a network test command.
3
Wait for the response from the target.
4
If a reply is received, report success with timing details.
5
If no reply is received, report failure or unreachable host.
💻

Code

powershell
param(
  [string]$Target = "8.8.8.8"
)

try {
  $result = Test-Connection -ComputerName $Target -Count 2 -ErrorAction Stop
  $result | Format-Table -Property Source, Destination, IPV4Address, Bytes, Time, TTL
} catch {
  Write-Output "Failed to reach $Target: $_"
}
Output
Source Destination IPV4Address Bytes Time(ms) TTL ------ ----------- ----------- ----- -------- --- YourPCName 8.8.8.8 8.8.8.8 32 20 117 YourPCName 8.8.8.8 8.8.8.8 32 21 117
🔍

Dry Run

Let's trace testing connectivity to 8.8.8.8 through the script

1

Input Target

Target = '8.8.8.8'

2

Send Ping

Test-Connection sends 2 ping requests to 8.8.8.8

3

Receive Response

Replies received with timing and TTL info

Ping #SourceDestinationBytesTime(ms)TTL
1YourPCName8.8.8.83220117
2YourPCName8.8.8.83221117
💡

Why This Works

Step 1: Send Ping Requests

The Test-Connection command sends ICMP echo requests to the target to check if it is reachable.

Step 2: Wait for Replies

It waits for replies and measures the time taken for each response.

Step 3: Report Results

If replies come back, it shows details like source, destination, bytes, time, and TTL; otherwise, it reports failure.

🔄

Alternative Approaches

Using System.Net.NetworkInformation.Ping Class
powershell
param([string]$Target = "8.8.8.8")
$ping = New-Object System.Net.NetworkInformation.Ping
$reply = $ping.Send($Target)
if ($reply.Status -eq 'Success') {
  Write-Output "Ping to $Target successful: Time=$($reply.RoundtripTime)ms"
} else {
  Write-Output "Ping to $Target failed: $($reply.Status)"
}
This method gives more control over ping options but requires more code.
Using Test-NetConnection Cmdlet
powershell
Test-NetConnection -ComputerName 8.8.8.8 -InformationLevel Quiet
Simpler command that returns True/False for connectivity but less detailed info.

Complexity: O(n) time, O(1) space

Time Complexity

The time depends linearly on the number of ping requests sent (n). Each ping waits for a reply or timeout.

Space Complexity

The script uses constant extra memory to store ping results and output.

Which Approach is Fastest?

Using Test-NetConnection is fastest for simple checks, while Test-Connection provides detailed info with moderate overhead.

ApproachTimeSpaceBest For
Test-ConnectionO(n)O(1)Detailed ping info with multiple attempts
System.Net.Ping ClassO(n)O(1)Custom ping control and handling
Test-NetConnectionO(1)O(1)Quick connectivity check with simple output
💡
Use -Count to limit the number of ping attempts and avoid long waits.
⚠️
Beginners often forget to handle errors, causing the script to stop if the host is unreachable.