Script to automatically refresh my WiFi connection


  1. Posts : 3,513
    Windows 10 Pro 64-bit 21H1 (May 2021 build 19043.1083)
       #1

    Script to automatically refresh my WiFi connection


    That said, is there a script I can run to automatically refresh my WiFi connection? I use my computer at home from work via AnyDesk. The home computer is connected to the internet using a USB WiFi dongle. Sometimes the connection is very slow and non-responsive. This is not because of AnyDesk, I have tested it locally at home. To fix it I must switch off WiFi for 2-3 seconds and then reconnect. Obviously I cannot do that remotely via AnyDesk or I won't be able to reconnect. Is there any script I could run to do that for me? Disconnect temporarily (even for 1 sec) and then reconnect automatically so I will be able to connect remotely again?

    Thank you for advance.
      My Computer


  2. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #2

    Hello @spapakons,

    spapakons said:
    That said, is there a script I can run to automatically refresh my WiFi connection?

    Is there any script I could run to do that for me? Disconnect temporarily (even for 1 sec) and then reconnect automatically so I will be able to connect remotely again?
    Here is something that I wrote for ME [ but it might help you ] that does this for Wi-Fi whenever I need to Disable and then Enable it.

    First though, you will need to find out what the actual Wi-Fi Name is.

    To do that, in a CMD prompt, Copy & Paste the following command and press Enter . . .

    Code:
    
    PowerShell "Get-NetAdapter -Name * | Select Name, InterfaceDescription, Status | Format-Table -AutoSize | Out-String -Width 1000"

    Then change Wi-Fi in set "Name=Wi-Fi" in the below Script to that of the Name specified in the above output and save it as a .bat file. This will work if you have more than ONE Wi-Fi adapter as you are hardcoding the Name into the Script . . .

    Code:
    
    @echo off
    if not "%1"=="max" start /max cmd /c %0 max & Exit/b
    :: ###############################################
    :: # Title     : Check and Reset Network         #
    :: # Created By: Paul Black                      #
    :: # Created On: 17-May-2022                     #
    :: ###############################################
    set "Title=Check and Reset Network" & set "Author=Paul Black"
    title %Title% - Written by %Author%.
    
    setlocal EnableDelayedExpansion
    fsutil Dirty Query %SystemDrive% > nul && goto:[RunAs]
    echo CreateObject^("Shell.Application"^). ^
    ShellExecute "%~0","+","","RunAs",1 > "%Temp%\+.vbs" && "%Temp%\+.vbs" & Exit
    :[RunAs]
    
    set "Name=Wi-Fi"
    
    :Options
    
    echo. & echo  ======================================================================================================================================================================
            echo  %Title%.
            echo  ======================================================================================================================================================================
    echo. & echo  Options:
    echo. & echo  [0] EXIT this Program.
    echo. & echo  [1] Run Current Network Status.
            echo  [2] Run Reset Network Adapter [ Wi-Fi ].
    echo. & echo  ======================================================================================================================================================================& echo.
    
    CHOICE /N /C 012 /M ">Enter an Option number:"
    
    if %ErrorLevel%==3 cls & goto :Reset_Wi-Fi
    if %ErrorLevel%==2 cls & goto :Check_Network_Status
    if %ErrorLevel%==1 Exit
    
    :Check_Network_Status
    
    echo. & echo  Processing [ Current Network Status ] . . .
    echo. & echo  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------& echo.
                  wmic nic get name, index
                  netsh interface show interface
                  ipconfig /all
    echo. & echo  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    echo. & echo  Processing Complete.
    echo. & echo ^>Press ANY key to return to the Options . . . & pause >nul & cls & goto :Options
    
    :Reset_Wi-Fi
    
    echo. & echo  Processing [ Resetting Network Adapter ] . . .
    echo. & echo  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
                  netsh interface set interface "%Name%" disable >nul
                  netsh interface set interface "%Name%" enable & timeout /t 3 /nobreak >nul
            echo  Current Network Adapter Status:
                  netsh interface show interface
            echo  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    echo. & echo  Processing Complete.
    echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & Exit
    
    
    

    I hope this helps.
    Last edited by Paul Black; 19 May 2022 at 11:16. Reason: Tidied Script Up.
      My Computer


  3. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #3

    spapakons said:
    That said, is there a script I can run to automatically refresh my WiFi connection?

    Disconnect temporarily (even for 1 sec) and then reconnect automatically so I will be able to connect remotely again?
    I have a script.
    - It disables then re-enables the WiFi adapter
    - It automatically detects the WiFi adapter name then uses that in the relevant commands.
    - It assumes that there is only one WiFi adapter. This avoids having to include checks of multiple adapters being detected.
    - I run it from an Admin shortcut set to run minimised.
    - The two netsh commands are the ones that necessitate the Admin permission.
    - I do not need any delay between the disable & re-enable commands but I added a remarked out one for you to experiment with.
    - There is no user interaction because none is required.
    - I have been using this script for at least six years on multiple computers without any glitches occurring.

    DisableEnableWiFiCard.bat

    Code:
    :: This script will enable-disable the last-found WiFi adapter name so is really only suitable for single-WiFi-adapter computers 
    :: Written by Denis, Try3
    :: The WiFi switch, if one exists, must be turned on
    :: I always run this using an Admin shortcut set to run minimised so I don't bother suppressing with @echo off
    
    prompt $g
    
    Set /a Found=0
    set FindString=Name
    Set UseExpresssion=netsh wlan show interfaces 
    for /F "tokens=*" %%X IN ('%UseExpresssion%  ^|   find /i "%FindString%"') do Call :Interfaces "%%X"
    GoTo EndGetWiFi
    
    :Interfaces
    :: Detect the WiFi adapter name
    Set ThisLine=%~1
    Set AdapterName=%ThisLine:*: =%
    GoTo :EOF
    
    :EndGetWiFi
    :: Do the disabling-enabling
    netsh interface set interface "%AdapterName%" disabled
         :: If you need a delay between disabling and re-enabling then this is where you'd put it
         :: Sample delay 1 second
         :: TIMEOUT /T 1 >nul
    netsh interface set interface "%AdapterName%" enabled
    ::Pause to check outcome during testing

    All the best,
    Denis
      My Computer


  4. Posts : 3,513
    Windows 10 Pro 64-bit 21H1 (May 2021 build 19043.1083)
    Thread Starter
       #4

    Thank you for your quick reply. I would rather not risk it now that I am remotely connected via AnyDesk. I will test it later at home.
      My Computer


  5. Posts : 9,790
    Mac OS Catalina
       #5

    Any VPN connection is going to be slow because Upload speeds are hindering along with many other factors. I have yet to see any company provided VPN type connection that is not slower than crap.
      My Computer


  6. Posts : 3,513
    Windows 10 Pro 64-bit 21H1 (May 2021 build 19043.1083)
    Thread Starter
       #6

    That's why you use VPN only to access sites blocked by your ISP. I cannot tell more without getting in trouble...
      My Computer


  7. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #7

    Hello @spapakons,

    Was the code that Denis and I gave you any good?
      My Computer


  8. Posts : 3,513
    Windows 10 Pro 64-bit 21H1 (May 2021 build 19043.1083)
    Thread Starter
       #8

    Haven't tested yet. I just turn off and on WiFi before leaving home.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 07:52.
Find Us




Windows 10 Forums