Check Internet Connectivity using Powershell

Handian Sudianto 5,161 Reputation points
2024-07-19T00:53:03.6533333+00:00

Hello,

I want to make a powershell script to test is the server have internet connectivity or not by doing ICMP to 8.8.8.8 for example and the script will return 1 if get reply and 0 if not get the reply. Anyone know here how to make that script?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,549 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,583 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,811 Reputation points
    2024-07-19T02:25:10.08+00:00

    You can use something like this:

    $pp = $ProgressPreference
    $ProgressPreference = 'SilentlyContinue'
    test-netconnection -ComputerName 8.8.8.8 -InformationLevel Quiet
    $ProgressPreference = $pp
    

    But why the need for a 1 or 0 as the result? The Test-NetConnection will return either $True or $False (if the -InformationLevel parameter is set to "Quiet") which is easier to understand.

    The annoying thing with this cmdlet is the "progress" information sent to the console. You have to manipulate the $ProgressPreference variable to suppress that.

    If you use the Net-Connection frequently in a script you'd do well to create a function so you don't have to repeatedly set and reset the $ProgressPreference variable.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.