≡ Menu

Powershell: Verify network connectivity

There are several ways to verify if a computer is connected to network. Some prefer querying the IP address details, enable status, and ping test to default gateway. Each kind of approach has its advantages and some kind of approaches are preferred based on the requirements.

In this post I will show you easiest way I found to verify the network connectivity status of a computer.

The System.Net.NetworkInformation.NetworkInterface class has a method called GetIsNetworkAvailable to verify the network connection status of the local computer. This method returns True when the network connectivity is available and False when no network connectivity found.

Look at the below example for better understanding.

if([System.Net.NetworkInformation.NetworkInterface]::GetIsNetworkAvailable()) {            
    Write-Host "$env:ComputerName connected to network"            
} else {            
    Write-host "$env:ComputerName not connected to network"            
}

Hope this tip helps..

Let me know if you come across any other easy/better way.