Stupid Windows Tricks: Restart network adapter when it hangs

Occasionally, my net connection will go pear-shaped — the wireless will just die. This happens most when I am uploading data faster than the cable connection can handle. (Like, say, when I am using VNC from work into home while I am also transferring data.) Without manual intervention, it’ll just sit there connectionless. If I’m not at home, I’m left offline until I can get home and manually click the Repair button.

This annoys me to no end. So, I fixed it.

Save the following as testnet.bat and put it somewhere useful. Download devcon from Microsoft and save it in your path.

@echo off
echo Testing net connection ...
netsh diag ping adapter 1 & if errorlevel 1 goto reset
goto done
echo Restarting Wireless Network Drivers ...
echo %DATE% %TIME% Restarted wireless drivers. >> c:\\batch\\testnet.log
rem netsh interface set interface "Wireless Network Connection" DISABLED DISCONNECTED
rem netsh interface set interface "Wireless Network Connection" ENABLED CONNECTED
c:\\batch\\devcon.exe restart =NET @pci\\VEN_14E4*
echo Done.

Schedule the batch file to run every 5 or 10 minutes. The kicker is, make sure you schedule it as an admin user that you do not use to log in, such as Administrator. This way, the annoying little popup window doesn’t disturb your day. (Yes, you can run it as yourself if you create a PIF that runs the window minimized, but then you have a window appearing and disappearing from your task bar all day.)

Make It Work For You

  1. Change the netsh diag line to use the adapter that works for you. More often than not, it will be adapter 1, but you can run netsh diag show adapter to get a list of adapter numbers for your system.
  2. Change the log file location to someplace that you can find.
  3. Change the full path to devcon.exe to whatever you saved it as.
  4. Change the device identification string from @pci\VEN_14E4 to the one that fits your network card. You can run devcon find =NET to get a list of network card vendor strings. You don’t need the whole thing, just enough to uniquely identify it, then an asterisk as a wildcard.
  5. If your primary DNS server is not your own (such as if it is your ISP’s) then you may want to not ping it. You can change the netsh diag ping line to ping a specific IP instead, such as the external address of your cable/DSL modem. This will look like netsh diag ping iphost myhostname. This has the added benefit of not bombing out your LAN connectivity if it is a WAN connectivity problem.

How It Works

  1. The netsh diag command pings three hosts: the default gateway, the primary DNS server, and the local non-localhost IP. On success, errorlevel is 0.
  2. If the pings are not successful, the reset part of the file is executed. The devcon utility just restarts the network card driver, basically doing the same thing as if you had gone into the Device Manager and disabled then enabled the card.
  3. That rem lines should have worked, but Microsoft apparently crippled the netsh command so that it could no longer do what its own help file says it can do. Hence why you need devcon instead.
Categorized as Random, Web

By Rick Osborne

I am a web geek who has been doing this sort of thing entirely too long. I rant, I muse, I whine. That is, I am not at all atypical for my breed.