Introduction
Many Windows users who upgraded their computers to Windows 10 have found they have a problem with Windows intermittently not automatically reconnecting to their WiFi router after startup, sleep, or hibernation. There are a lot of possible solutions offered online for this – from running chkdsk
, to ensuring the home network is set to 'Private
', to updating network drivers, to disabling power management on the network adaptor in Device Manager, to turning off 'fast start-up' in Power Options – and one or more of these solutions may work for you, so please try them first.
But many people have jumped through all of those hoops to no avail and the only advice I've seen after that is to reinstall Windows from scratch – which seems to me like rebuilding your house because the front door is sticking! So I decided to go down a different route and write a batch script to check whether the home WiFi router is connected and try to connect to it if not. Then I scheduled a task to run the script on startup or resume.
I hope this article will help anyone else facing this problem.
Requirements
A Windows admin account will be needed to run the script and to schedule a task to run regardless of whether any user has logged on or not. If you don't have an admin account, you will need to speak to your network administrator.
Using the Code
Paste this code into a batch file (given the extension .bat) and edit 'HOME-ROUTER
' to your own router's SSID
.
@ECHO OFF
SETLOCAL EnableDelayedExpansion
set LOGFILE=output.txt
call :CHECK >> %LOGFILE%
exit /b
:Check
ECHO ---------------------------------------------
ECHO Start check at %date% %time%
set "myRouter=HOME-ROUTER"
set "host=8.8.8.8"
set counter=1
:WHILE
if !counter! EQU 4 goto :failpoint
ping -n 1 "!host!" | findstr /r /c:"[0-9] *ms"
if !errorlevel! EQU 0 (
if !counter! EQU 1 (
ECHO Already connected
) else (
ECHO Connected OK
)
goto :exitpoint
) else (
ECHO Not connected
ECHO Connection attempt !counter!
netsh wlan connect !myRouter!
timeout 5
set /a counter+=1
goto :WHILE
)
:failpoint
ECHO Failed three connection attempts
:exitpoint
Save the file somewhere on your computer and test it directly by disconnecting your WiFi and running the batch file to see if it reconnects the WiFi okay. Once you have established that it does, you can go ahead and create a scheduled task to run it at the appropriate times.
Notes
I've used one of Google's DNS IP addresses (8.8.8.8) as a reliable host to check, but you could equally use the IP address or share name of any local device (such as a wireless printer – e.g. 192.168.1.29 or 'HP_LaserJetPro') if you know it will always be available. This may be preferable if your internet connection is unreliable or 'on demand'.
In no way am I an expert with the Windows command line and/or batch file coding. I've simply put this code together – after a lot of head-scratching and hair-pulling – from snippets I've found online. So it may not be the most efficient, robust, or effective way to accomplish this task. I am always open to constructive criticism and advice on how to improve it.
Schedule the Task
- Start the Task Scheduler (press the Windows key + R to open the Run box, type taskschd.msc and press Enter).
- Click on Create Task… in the 'Actions' pane.
- Name the task.
Tip: Start your own task names with an underscore so they appear at the top of the list. - Under 'Security options' select Run whether user is logged on or not.
Note: If you are not currently logged into Windows with an admin account, you will need to change the user to an admin account by clicking the Change User or Group… button. - Check the Run with the highest privileges box.
- Go to the 'Triggers' tab and add two New… triggers:
- For system startup:
- Select At startup from the drop-down list at the top.
- Check the box Delay task for: and set it for 30 seconds.
This helps to ensure Windows has started the services necessary for the network connection to be established. - Click OK.
- For resuming from sleep or hibernation:
- Select On an event from the drop-down list at the top.
- Select System from the 'Log' drop-down.
- Select Kernel-Power from the 'Source' drop-down.
- Enter 107 in the 'Event ID' box.
- Check the box Delay task for: and set it for 30 seconds.
The delay here may be unnecessary, but it can't do any harm. - Click OK.
- Go to the 'Actions' tab and add this New... action:
- Select Start a program from the 'Action' drop-down.
- Enter the full path and script name in the 'Program/script' box – or use the 'Browse…' button to locate it.
- In the Start in (optional): box, enter the path where you want the output.txt log file to be created, even if it's the same path as the batch file. Otherwise, it won't get created at all.
Alternatively, you could edit the LOGFILE constant in the batch file itself, to give it a full path. - Click OK.
- Go to the 'Conditions' tab and uncheck the option Start the task only if the computer is on AC power.
- Click OK.
- When prompted, enter the password for the selected admin account.
That should be it. Test every restart scenario and ensure the log file 'output.txt' is being updated each time.
History