Quick Hits: Test Internet Access

A while back I was having all sorts of internet connection issues with my ISP.  The connection would drop and I would have to play the “click refresh” game with my browser and wait until the magical moment when I would have my access back.

So with that, I came up with an idea to monitor my connection to a website that would let me know when that access came back, saving me the time and annoyance of click the refresh button.

I wouldn’t say this is the most practical script, but it was something I decided to do just to kill some time.

I use two main cmdlets to accomplish this task:

Test-Connection with the –count and –quiet parameters

Write-Host with the –foregroundcolor and  -nonewline parameters

Using the –count and –quiet parameters will attempt one try at connecting to the computer, or in this case the website. The –quiet allows me to return a True (if the site is reachable) and a False (if the site is not reachable).


For the sake of demonstration, I am disabling my network connection and using google.com as my test connection site. Now why didn’t I use a site like bing.com or microsoft.com?  Good question! For whatever reason, the Test-Connection fails against either of these sites while google.com returns a positive connection.

Test-InternetConnection -Site google.com -Wait 2


That’s all there is to it. A date/time stamp is also given when the connection has come back up.


Function Test-InternetConnection {
    Use this function to test your internet connection and monitor while it is down.
    Use this function to test your internet connection and monitor while it is down. When the connection is back up, a text notification will show up.
    Name of the site to use for connection test.
    Time to wait before retrying Test-Connection (seconds)
    Author     : Boe Prox
    Created    : 05Feb2011
    DefaultParameterSetName = 'Site'
        Mandatory = $True,
        ParameterSetName = '',
        ValueFromPipeline = $True)]
        Mandatory = $True,
        ParameterSetName = '',
        ValueFromPipeline = $False)]
    #Clear the screen
    #Start testing the connection and continue until the connection is good.
	While (!(Test-Connection -computer $site -count 1 -quiet)) {
        Write-Host -ForegroundColor Red -NoNewline "Connection down..."
        Start-Sleep -Seconds $wait
    #Connection is good
	Write-Host -ForegroundColor Green "$(Get-Date): Connection up!"
This entry was posted in powershell, scripts and tagged , , . Bookmark the permalink.

10 Responses to Quick Hits: Test Internet Access

  1. Bernd says:

    This is awesome, thanks. I added some code to show a tray area balloon notification:

    [void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
    $objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon
    $objNotifyIcon.Icon = “d:\code\inetcheck\check.ico”
    $objNotifyIcon.BalloonTipIcon = “Info”
    $objNotifyIcon.BalloonTipText = “Online”
    $objNotifyIcon.BalloonTipTitle = “Online”

    $objNotifyIcon.Visible = $True

    Test-InternetConnection -Site google.com -Wait 2


  2. Vladimir says:

    While (!(Test-Connection -computer google.com -count 1 -quiet)) {
    Write-Host -ForegroundColor Red -NoNewline “Connection down…”
    Start-Sleep -Seconds 2
    #Connection is good
    Write-Host -ForegroundColor Green “$(Get-Date): Connection up!”

    This code is good, but slow … What about multithreading of powershell? Ping in some streams like Dive Computers networks and the availability of DNS hand on it?

  3. Vladimir says:

    Hello, again. I have finished my project. Please tell me how to convert “ps1” in the “EXE”?

  4. Vladimir says:

    I am grateful to you for the code. I’m trying to find computers on my network that have access to Internet use. With the “Default Gateway” and “DNS server”. My code takes the IP addresses of the computers on my network with a text file that is pre-filled with IP addresses to computers on my network and in turn checks for free access to the internet.

  5. Vladimir says:

    Hello, I’m from Ukraine and I do not speak very well English. I need your assistance conditional. Your code that you have written a very big and heavy. Is there an alternative? this is the code to check the Internet connection? I need a code that simply checks the internet connection on my PC.

    this code does not suit me. it is not working properly:

    $ net = (ipconfig | Select-String “$ Server”-Quiet)
    if (“$ net”-eq ‘true’)
    Write-Host “connecting!” -BackgroundColor Green
      Write-Host “but connect!” -BackgroundColor Red

    • Boe Prox says:

      The actual code within my example that performs the operation isn’t really that big or heavy (only 6 lines of code):
      While (!(Test-Connection -computer google.com -count 1 -quiet)) {
      Write-Host -ForegroundColor Red -NoNewline “Connection down…”
      Start-Sleep -Seconds 2
      #Connection is good
      Write-Host -ForegroundColor Green “$(Get-Date): Connection up!”

      What are you trying to do with the code you provided? I see a $server variable but am not sure what that represents…

  6. Chris Brown says:


    It’s interesting that you post this, as I use a similar process to monitor websites at work. We have a fairly strict network, so pinging things was out of the question. As a result of this I came up with a better solution – download the page! I grab the content of the page, then search it for a particular string. This way things like 500s and 403s can be detected and acted on immediately, rather than only knowing about the whole site outage.

    This is, in turn, used to monitor our internet connection (by way of http://www.google.com). Our proxy returns a lovely error message, so simply ensuring a 200 is returned was not enough.

    • boeprox says:

      Hi Chris, Thanks for the comment. I am assuming your using the System.Net.WebClient or the System.Net.WebRequest class to perform the download of the content of the page? That is actually what I also have set up at work. Although we use that to monitor some Intranet pages instead of internet access. That is a better way of monitoring a specific site for internet connectivity, however, I wanted to keep it more simple using the Test-Connection to keep it in line for my ‘Quick Hits’ series. But using the other way does lend itself to being a nice post for later on. 🙂

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s