I have a media server (Windows 8) located in a very inconvenient part of my house. Occasionally I will have to reset my router or it will reset itself. The issue is the PC loses connectivity for some reason, and I am forced to walk outside, around the house, into the basement, over a bunch of toys and weights and boxes, to push a button to reboot it.

I would love to have it check itself every 5-10 minutes and auto reboot if it is unable to ping a given address/IP.

Any ideas how to accomplish this?


Batch File:



if (!(Test-Connection -quiet)) {Restart-Computer -Force}


 If Reachable("") Then
  WScript.Run("shutdown -r -t 00")
 End If

 Function Reachable(strComputer)
  Dim wmiQuery, objWMIService, objPing, objStatus
  wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputer & "'"

  Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
  Set objPing = objWMIService.ExecQuery(wmiQuery)

  For Each objStatus in objPing
      If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
          Reachable = False 'if computer is unreacable, return false
          Reachable = True 'if computer is reachable, return true
      End If
 End Function

Any of these can be scheduled to work as a task at an intervall to suit you - an if scheduled to run with "highest priviledges", then will be able to overcome any potential UAC problems.

Bear in mind this only checks connectivity to your router. It maybe worth replacing the IP with one such as www.google.com or similar.. make it something which you know replies (do a manual ping) and make sure it isnt a dodgy site which is vulnerable to downtime.. you dont want your pc rebooting for nothing

| improve this answer | |

Here is a PowerShell script that will do it. Just run it as a scheduled task as frequently as you want. It uses Restart-Computer, you may need to use -force, depending on circumstances. Check the link for options.

If (!(Test-Connection -quiet)) {
    #Write-Host "Not connected"
Else {
    #Write-Host "Connected"

I included a full If/Else statement, however you can do this in one line. The full If/Else gives you the flexibility to do more if needed. Here is a 1 liner:

if (!(Test-Connection -quiet)) {Restart-Computer}

If you cannot ping your router when it fails, use your router's address in the script. That would be the best option. You could test a website like yahoo.com. The problem with testing an external site is that:

  1. The remote address has to accept pings
  2. The remote address might be down, which would make the machine reboot
| improve this answer | |
  • just a quick note on this... add the -Force switch to restart-computer. If you do not, the machine will not reboot if a service stops it or if you have an inactive RDP session (or various other reasons). The -Force switch will ignore all of these and make it reboot – Fazer87 Jul 11 '19 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.