Automatically restart computer when internet connection is lost

I have some trouble to do a script for Windows 10 that will check my internet connection every 2 minutes and restart the computer if it doesn’t find any connection.

I tried with PowerShell:

mainLoop() 
sleep 120
{
    if (!(Test-Connection 8.8.8.8 -Quiet)) {
        #Write-Host "Not connected"
        Restart-Computer
    } else {
        mainLoop()
    }
}

Do you have some advice to make it works?

  • 1

    Well – you’re not looping for one.

    – 

  • Call your function in a while($true) loop and sleep after testing the connection if it’s still connected, for starters.

    – 

  • 1

    Restart-Computer is somewhat drastic in response to a loss of connection — isn’t it?

    – 

  • 2

    I would ping 192.168.1.1 first to check if it’s not your router/modem needing a reboot. Also have a look at netsh winsock reset and ipconfig options for a faster/softer fix.

    – 

Loop while the remote host is reachable, and restart once the loop terminates:

while (Test-Connection 8.8.8.8 -Quiet) {
    Start-Sleep -Seconds 120
}
Restart-Computer

Leave a Comment