Monthly Archives: Jan 2015

Windows batch script for auto reconnection to wireless network


Recently I had a client with an odd problem – a laptop randomly disconnecting from the WLAN connection. After ruling out all the possible software issues – OS setup, network configuration, drivers, etc. as well as the access point I was left with no choice but to think of something to go around this. And what I came up with was a looped batch script using netsh command. Now some of you may ask – why not powershell, it is modern – well I do not know much about it and from what I have read about network interface manipulation one could only enable or disable the interface, but that was not what I wanted anyway.

Just a bit of info for those who are not familiar with netsh command, to make the script working you will need some information regarding your wireless setup. You will need the SSID, profile name and the name of your WLAN interface. Using netsh command you can:

C:\>netsh wlan show all – this will give you the full information in relation to wireless network and all the info needed, but if you are not interested in all the details you can be more specific.

C:\>netsh wlan show networks – to find the SSID

C:\>netsh wlan show profile – to find the profile name

C:\>netsh wlan show interfaces – to find the name of the interface

Most likely your SSID and profile name are going to be the same. Of course you can just left click on the network connections icon in task bar’s notification area, move the pointer over the connected wireless network and in the popup message you will see your profile name and SSID. In Windows 7 the WLAN interface name is “Wireless Network Connection”, Windows 8 – “Wi-Fi”.

Now to the batch script, what it basically does is to check if default gateway exists and if it doesn’t – the netsh command to reconnect is executed. The script is looped, so it runs itself constantly with a delay you can adjust:



for /f “tokens=1-2 delims=:” %%a in (‘ipconfig^|find “Default”‘) do if not [%%b]==[] goto restart

netsh wlan connect ssid=”Wireless Network” Name=”Wireless Network” Interface=”Wireless Network Connection”


timeout /t 5

goto loop


Copy/Paste it in let say Notepad and save it as .bat file /BTW when copy/paste from the web content make sure you got the lines correctly/. Adjust it to your needs and to stop it you can interrupt it with Ctrl+C.

There is an annoying inconvenience thought – when you run the script the Command Prompt window is visible, but there is luckily a solution for that – wrapping it in a VBS /found this solution on internet, thanks to a man who’s name cannot sadly remember/ and run it in the background. So the VBS file looks like this:


Dim WinScriptHost

Set WinScriptHost = CreateObject(“WScript.Shell”)

WinScriptHost.Run Chr(34) & “C:\PathTo\mybat.bat” & Chr(34), 0

Set WinScriptHost = Nothing


Copy/Paste and change the path to the .bat file accordingly and save the file as .vbs. Now every time you need to keep your WLAN connection alive – start the VBS file. If you think it will be convenient you can add it to your start up items.

And that’s all. 🙂