[Batch] Toggle you Wi-Fi On or Off

Discussion in 'Scripting' started by Dos_Probie, Mar 8, 2014.

  1. Dos_Probie

    Dos_Probie MDL Senior Member

    Jul 18, 2012
    250
    86
    10
    #1 Dos_Probie, Mar 8, 2014
    Last edited by a moderator: Apr 20, 2017
    Here is a simple batch script that checks your active wi-fi profile then will toggle your wireless connection either on or off depending on the connection.
    DP..:cool:
    Code:
    @set @jscript=1/*
    @echo off&color a&mode con: cols=50 lines=3&title, [ Toggle Wireless Connection ]
    :: Toggle.Wi-Fi.cmd - Windows 7/8/8.1 - DosProbie - 2014.03.07
    
    :: ADMIN CK
     reg query "hku\S-1-5-19" >nul 2>&1 || (
     @cscript //e:jscript //nologo "%~f0" "%~f0"
     goto :eof
     )
    
    :: SET ACTIVE PROFILE TO VARIABLE
     for /f "delims=" %%a in ('reg query "HKLM\software\microsoft\windows nt\currentversion\networklist\Profiles" ^| Find "{"') Do Set var=%%~nxa&goto :getssid
    :getssid
     for /f "tokens=2,*" %%b in ('reg query "HKLM\software\microsoft\windows nt\currentversion\networklist\Profiles\%var%" ^| Find "ProfileName"') Do Set ssid=%%%c
     cls
    
    :: CK CONNECTION
     ping -n 1 yahoo.com >nul
     if Errorlevel 1 GoTo enable
    
    :disable
    :: DISABLE WI-FI
     netsh wlan disconnect >nul 
     cls
     echo Connection Disabled..&& timeout /t 2 >nul
     goto end
    
    :enable 
    :: ENABLE WI-FI
     netsh wlan connect %ssid% >nul 
     cls
     echo Connection Enabled..&& timeout /t 2 >nul
    
    :end
     exit
    :eof
    
     // RUNAS ADMIN
     */
     var strArg = WScript.Arguments(0);
     var objSH = WScript.CreateObject("Shell.Application");
     objSH.ShellExecute(strArg, "", "", "runas", "5");
    
     
  2. ianymaty

    ianymaty MDL Member

    Jan 31, 2010
    138
    98
    10
    Nice one. I can't test it now. I don't have Wi-Fi in my computer.

    I may need it so I'll save it and test it when I get a chance.

    Thank you.
     
  3. Dos_Probie

    Dos_Probie MDL Senior Member

    Jul 18, 2012
    250
    86
    10
    I also have another batch script I did a few years ago that will toggle your lan connection as well, Also this Wireless script
    does require for you to setup your wireless for your local computer, with a ssid and password to connect automatically.
    DP..:cool: