Monitor your Connection to Internet – PowerShell

Recently I’ve been having issues with my internet connection.  So I decided to Write a script to monitor my connection and then record how long my connection drops to my Internet Service Provider.

To start this process out I had to make sure that I could ping the gateway of the adapter that I’m using to connect to the internet with.  So first step was to find my IP address and gateway.  I was able to do this using Get-NetIPConfiguration.

Get-NetIPConfiguration -InterfaceAlias 'vEthernet (ExternalSwitch)'

InterfaceAlias : vEthernet (ExternalSwitch)
InterfaceIndex : 11
InterfaceDescription : Hyper-V Virtual Ethernet Adapter
NetProfile.Name : Conn
IPv4Address :
IPv6DefaultGateway :
IPv4DefaultGateway :
DNSServer :

This told me my address and my Gateway.  So I put them in a variable:

$IP = (Get-NetIPConfiguration -InterfaceAlias 'vEthernet (ExternalSwitch)').ipv4address.ipaddress

$gateway = (Get-NetIPConfiguration).ipv4defaultGateway.nexthop

Now that I have them in a variable I can begin the process of  Pinging both addresses.  I chose to add this to a Function:

function Start-ConnectionMonitoring
param($isp, $gateway, $Logfile,[int]$Delay = 10,[Ipaddress] $adapter, [switch]$ispPopup, [switch]$gateWayPopup)
$spacer = '--------------------------'
if(!(Test-Connection $gateway -source $adapter -count 1 -ea Ignore))
get-date | Add-Content -path $Logfile
"$gateWay Connection Failure" |add-content -Path $Logfile
$outagetime = Start-ContinousPing -address $gateway -adapter $adapter -Delay $Delay
"Total Outage time in Seconds: $outageTime" | Add-Content -path $Logfile
New-PopupMessage -location $gateway -outagetime $outagetime
$spacer |add-content -Path $Logfile
if((!(Test-Connection $isp -Source $adapter -count 1 -ea Ignore)) -and (Test-Connection $gateway -count 1 -ea Ignore))
get-date | Add-Content -path $Logfile
"$isp Connection Failure" | Add-Content -Path $Logfile
$outagetime = Start-ContinousPing -address $isp -adapter $adapter -Delay $Delay
"Total Outage time in Seconds: $outageTime" | Add-Content -path $Logfile
New-PopupMessage -location $isp -outagetime $outagetime
$spacer|add-content -Path $Logfile
Start-Sleep -Seconds $Delay

In this function I have two Nested functions.  I’ll explain the first function(Start-ContinousPing).  If the connection/ping to either the local router ($gateway) or ($isp) fails then we call this function.  This puts the Ping/connection check in a loop until the connectivity comes back. At the end of the non connectivity the function passes back the seconds that we couldn’t reach that resource.

The Second function (New-PopupMessage) serves as a means to allow the user to choose whether or not they get a popup when there is  a period of no activity.  If the switch -ispPopup is set then when we have no connectivity to the ISP resource we’ll get a popup indicating no connection and how long the connection was out.

Finally we’ll look at the contents of the log:

12/27/2017 4:59:29 PM Connection Failure
Total Outage time in Seconds: 0.0380652
12/27/2017 4:59:33 PM Connection Failure
Total Outage time in Seconds: 0.0353273

As you can see the connection to my first gate way was out of .038 seconds. Also the connection to my provider was out for .035 seconds.

The entire script is located in this gist:

I Hope this helps someone.


Until then

Keep Scripting



2 thoughts on “Monitor your Connection to Internet – PowerShell

  1. PaulieD

    Great script. One issue, as of 2020-Feb-13:

    Line 61 throws an error, due to missing argument:
    OLD LINE: new-item -Path $Logfile
    REPAIRED LINE: new-item -Path $Logfile -ItemType “file”

    To disable the pop-ups and just retain the logging, remove the “-IsPopup” from the last line of the script (line 65).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s