Todd Vanyo
Todd Vanyo

Reputation: 593

Windows: How to programatically connect to wireless networks?

With Windows 10 is it possible to setup up known networks and be able to connect to them without all the mouse movement and click?

Upvotes: 1

Views: 70

Answers (1)

Todd Vanyo
Todd Vanyo

Reputation: 593

Using Windows batch files, you can set it up to connect to networks you already know (Network1 or Network2, below) without ever touching the mouse.

@echo off
setlocal EnableDelayedExpansion
for %%i in ("Network1"
            "Network2") do (
    netsh wlan show networks mode=ssid | findstr /C:%%i
    if !ERRORLEVEL! EQU 0 (
        echo "Found %%~i - connecting..."
        netsh wlan connect name=%%i
        exit /b 
    ) else (
        echo "Did not find %%~i"
    )
)
@echo on

Save the above to .bat and run it from cmd.exe or a program like Listary.

Some comments about the code:

  1. If more than one of your listed networks are available, it will connect to whichever is first in the for loop list. You could also put the list in a file and change for %%i to for /F %%i
  2. EnableDelayedExpansion and "!" around ERRORLEVEL are needed to keep the variable ERRORLEVEL from being assigned whatever it was at the beginning of the script. Since I don't normally program Windows batch files, this is 2 hours of my life gone that you won't have to deal with.
  3. All the echoing is for debugging; the echo off at the top squelches it.
  4. %% needed for variables in Windows batch files. The variable is referenced with % at the command line.
  5. %%~i strips the quotation marks around the string when outputting to stdout.

Upvotes: 1

Related Questions