Testing if the Internet connection is working with PowerShell
Posted: (EET/GMT+2)
Quick PowerShell tip for today: if you need to test whether the Internet connection is available in your PowerShell script (for instance, prior to trying to download something), you can use the cmdlet called Test-NetConnection.
This command supports numerous parameters, but you can also run it without any parameters. The results will look something like this:
ComputerName : internetbeacon.msedge.net RemoteAddress : 204.79.197.200 InterfaceAlias : vEthernet (Intel(R) Centrino(R) Advanced-N 6235 Virtual Switch) SourceAddress : 192.168.100.35 PingSucceeded : True PingReplyDetails (RTT) : 91 ms
Although this isn't the case 100% of the time (because of corporate firewall rules, for example), you could read the PingSucceeded value to know if the Internet connection is (at least partially) up and running. You can also instruct Test-NetConnection to call HTTP web servers.
Here's the documentation for this command.