Suppress the Output of Test-NetConnection
If you’ve spent any time tinkering with PowerShell, you’ve probably run across the Test-NetConnection cmdlet. It’s a handy built-in tool that lets you check if a computer can reach another device over the network.
For example, if you want to see if you can connect to Google, you might type:
Test-NetConnection google.com
By default, PowerShell gives you a nice chunk of output that includes things like the remote address, ping results, and even traceroute information. That’s super helpful when you’re troubleshooting interactively, but what if you don’t actually want to see all that information?
Maybe you’re writing a script and only care about whether the command succeeds or fails. Or maybe you want the script to quietly do its thing in the background without cluttering up your screen. That’s where suppressing the output comes in.
Why Suppress Output?
When you’re learning PowerShell, the default output can feel like drinking from a fire hose. For scripting, less is often more. Suppressing unnecessary output makes your script cleaner and easier to read, while also helping you focus on the results that matter.
Option 1: Redirect the Output to $null
The easiest way to silence the output of Test-NetConnection is to send it straight to PowerShell’s "black hole," which is the special variable $null.
Test-NetConnection google.com | Out-Null
In this example, the results are thrown away, and nothing prints to the screen. The command still runs—it just doesn’t tell you about it.
Option 2: Assign the Output to a Variable
Another common trick is to capture the results in a variable. That way, the output isn’t shown, but you can still use it later if you want.
$result = Test-NetConnection google.com
Now your screen stays quiet, but you can inspect $result later in the script if you need to check whether the test passed:
if ($result.PingSucceeded) {
Write-Host "We can reach Google!"
} else {
Write-Host "No luck reaching Google."
}
Option 3: Redirect the Output Stream
PowerShell has multiple output "streams" (like success, error, warning, etc.). You can also redirect the success output (which is what Test-NetConnection normally sends) to $null:
Test-NetConnection google.com > $null
This has the same effect as Out-Null—no output on your screen.
Option 4: Assign Out to a Variable, Redux
This is a slight modification of the option 2, wherein we're using the ErrorAction and WarningAction parameters to specify the cmdlet should continue with the SilentlyContinue option. The ErrorAction and WarningAction parameters supress error messages and continues execution without displaying them. So, you'll still get the progress indicators up top, but you can handle displaying any output about successes and failures.
$Server="MyServer"
$Responding=(Test-NetConnection $Server -ErrorAction SilentlyContinue -WarningAction SilentlyContinue).PingSucceeded
if($Responding -eq $True){
write-host "$Server Responding" -ForegroundColor Green
} else {
write-host "$Server Not responding" -ForegroundColor Yellow
}
This would be handy in a loop, where you could be checking multiple machines, and maybe want to do further checks of the machines. Maybe such as, are certain services running or maybe configuration settings of particular machines.
Which Should You Use?
If you don’t care about the results at all → use | Out-Null or > $null.
If you want to keep the results for later use → assign them to a variable.
Test-NetConnection is a great tool, but when you’re scripting, its default output can sometimes be too much. By redirecting the results to $null, or storing them in a variable, you keep your scripts neat, tidy, and focused on exactly what you need.
Next time you’re writing a PowerShell script and want to keep things quiet, give these tricks a try!
About this post
Posted: 2025-09-15
By: dwirch
Viewed: 22 times
Categories
Scripting
Powershell
Beginners Guides
Attachments
Loading Comments ...
Comments
No comments have been added for this post.
You must be logged in to make a comment.