Network Operations
Note
Test connectivity, diagnose network issues, and query DNS from PowerShell with Test-Connection, Test-NetConnection, and Resolve-DnsName.
Overview
Before troubleshooting an application issue, you usually need to answer a basic question: can this machine even reach that one, on that port? PowerShell's network cmdlets answer that without shelling out to ping.exe or nslookup.exe and parsing text — they return structured objects you can filter, log, and act on directly.
Basic Syntax
Test-Connection -ComputerName "google.com"
Test-NetConnection -ComputerName "google.com" -Port 443
Resolve-DnsName -Name "google.com"
Invoke-WebRequest -Uri "https://example.com"
Key Points
Test-Connectionis PowerShell's ping — ICMP-based, useful for basic reachabilityTest-NetConnectionalso checks a specific TCP port, whichTest-Connectionalone can't do- Many networks block ICMP (ping) but allow specific ports —
Test-NetConnection -Portis often the more reliable check Resolve-DnsNameis PowerShell'snslookup, returning structured record objects
Basic Connectivity — Test-Connection
# Simple ping-style test (returns 4 replies by default in Windows PowerShell,
# a single Boolean-friendly summary in PowerShell 7+ with -Quiet)
Test-Connection -ComputerName "google.com"
# Just a true/false result — good for scripting conditionals
Test-Connection -ComputerName "google.com" -Count 2 -Quiet
# Ping multiple hosts and see which ones respond
$hosts = @("server01", "server02", "server03")
$hosts | ForEach-Object {
[PSCustomObject]@{
Host = $_
Online = Test-Connection -ComputerName $_ -Count 1 -Quiet
}
}
Output:
Port-Level Checks — Test-NetConnection
# Check if a specific port is reachable (this is what most app troubleshooting actually needs)
Test-NetConnection -ComputerName "sqlserver01" -Port 1433
# Common named checks (built-in shortcuts)
Test-NetConnection -ComputerName "smtp.example.com" -CommonTCPPort SMTP
# Include traceroute-style hop information
Test-NetConnection -ComputerName "google.com" -TraceRoute
# Quiet mode - just true/false for scripting
Test-NetConnection -ComputerName "sqlserver01" -Port 1433 -InformationLevel Quiet
Output:
Test-NetConnection Is the Right Tool for App Troubleshooting
"The app can't reach the database" is almost always a Test-NetConnection -ComputerName dbserver -Port 1433 question, not a Test-Connection (ping) question. A server can block ICMP entirely and still have port 1433 wide open — always test the actual port the application needs, not just basic reachability.
DNS Lookups
# Basic forward lookup
Resolve-DnsName -Name "google.com"
# Specific record type
Resolve-DnsName -Name "google.com" -Type MX
Resolve-DnsName -Name "google.com" -Type TXT
# Reverse lookup (IP to hostname)
Resolve-DnsName -Name "8.8.8.8"
# Query against a specific DNS server
Resolve-DnsName -Name "internal-app.corp.local" -Server "10.0.0.53"
Resolve-DnsName Is Windows-Only
Resolve-DnsName relies on the Windows DNS client and isn't available in PowerShell 7 on Linux/macOS. Cross-platform scripts should use [System.Net.Dns]::GetHostAddresses("google.com") instead, which works everywhere PowerShell runs.
Web Requests
# Basic GET request
$response = Invoke-WebRequest -Uri "https://example.com"
$response.StatusCode
# Check that an endpoint is up and responding correctly
$health = Invoke-WebRequest -Uri "https://api.example.com/health" -UseBasicParsing
if ($health.StatusCode -eq 200) {
Write-Output "API is healthy"
}
# Timeout so a hung endpoint doesn't hang the whole script
Invoke-WebRequest -Uri "https://slow.example.com" -TimeoutSec 10
See API Interactions for the Full Picture
Invoke-WebRequest and Invoke-RestMethod are covered in depth in API Interactions — this page focuses on using them for basic connectivity/health checks, not full REST API integration.
Network Adapter Information
# List network adapters
Get-NetAdapter
# Only active/connected adapters
Get-NetAdapter | Where-Object { $_.Status -eq "Up" }
# IP configuration
Get-NetIPConfiguration
# IP addresses assigned to this machine
Get-NetIPAddress | Where-Object { $_.AddressFamily -eq "IPv4" }
Important Parameters
| Parameter | Type | Description | Example |
|---|---|---|---|
-ComputerName | String | Target host | Test-Connection -ComputerName "srv01" |
-Port | Int | Test-NetConnection: TCP port to test | -Port 443 |
-Count | Int | Test-Connection: number of pings | -Count 2 |
-Quiet | Switch | Return only $true/$false | -Quiet |
-InformationLevel | String | Test-NetConnection: Quiet or Detailed | -InformationLevel Quiet |
-Type | String | Resolve-DnsName: record type (A, MX, TXT, etc.) | -Type MX |
-TimeoutSec | Int | Invoke-WebRequest: request timeout | -TimeoutSec 10 |
Common Patterns
# Pattern 1: Quick health check across a list of servers and ports
$targets = @(
@{Server="web01"; Port=443},
@{Server="db01"; Port=1433}
)
$targets | ForEach-Object {
$result = Test-NetConnection -ComputerName $_.Server -Port $_.Port -InformationLevel Quiet
[PSCustomObject]@{ Server=$_.Server; Port=$_.Port; Reachable=$result }
}
# Pattern 2: Wait for a service to become reachable (useful after a deploy/reboot)
$timeout = (Get-Date).AddMinutes(5)
do {
$up = Test-NetConnection -ComputerName "app01" -Port 8080 -InformationLevel Quiet
if (-not $up) { Start-Sleep -Seconds 10 }
} while (-not $up -and (Get-Date) -lt $timeout)
# Pattern 3: Resolve and validate a hostname before using it
$dns = Resolve-DnsName -Name "internal-app.corp.local" -ErrorAction SilentlyContinue
if (-not $dns) {
Write-Warning "Could not resolve internal-app.corp.local"
}
Real-World Examples
Example: Pre-Deployment Connectivity Check
Scenario: Before running a deployment script, verify the machine can reach every dependency (database, package feed, license server) it needs.
$dependencies = @(
@{Name="Database"; Host="db01.corp.local"; Port=1433}
@{Name="NuGet Feed"; Host="nuget.corp.local"; Port=443}
@{Name="License Srv"; Host="license01.corp.local"; Port=27000}
)
$results = $dependencies | ForEach-Object {
$ok = Test-NetConnection -ComputerName $_.Host -Port $_.Port -InformationLevel Quiet
[PSCustomObject]@{ Dependency=$_.Name; Host=$_.Host; Port=$_.Port; Reachable=$ok }
}
$results | Format-Table -AutoSize
if ($results | Where-Object { -not $_.Reachable }) {
throw "One or more dependencies are unreachable — aborting deployment"
}
Explanation: Failing fast with a clear per-dependency table beats discovering a blocked port halfway through a deployment. -InformationLevel Quiet keeps each check to a single Boolean so results compose cleanly into the summary table.
Tips & Tricks
-UseBasicParsing Avoids an IE Dependency
Older Windows PowerShell versions ofInvoke-WebRequest relied on Internet Explorer's engine for HTML parsing, which could fail on servers without IE configured. -UseBasicParsing skips that entirely — always safe to include, and irrelevant (default behavior) in PowerShell 7+. Test-Connection Being Blocked Isn't the Same as the Host Being Down
Don't conclude a server is down from a failed ping alone. Many production firewalls intentionally block ICMP while leaving application ports open — check the port that actually matters.Related Topics
- API Interactions - Full REST API request/response handling with Invoke-RestMethod
- Credential Management - Authenticating network requests securely