Skip to content

Instantly share code, notes, and snippets.

@andyneff
Created December 12, 2015 14:59
Show Gist options
  • Save andyneff/99a36d121cfce756ab11 to your computer and use it in GitHub Desktop.
Save andyneff/99a36d121cfce756ab11 to your computer and use it in GitHub Desktop.
Windows: Resetting network adapters matching a string pattern
:' Reset Networks devices matching %1 pattern
:' Example: To reset all virtual box network adapters "reset_adapters.bat VirtualBox"
:'^
a=1_
<1' echo off <nul
set n=nothing' & goto :'batchCode & REM Jump to the batch code
Set UAC = CreateObject("Shell.Application")
UAC.ShellExecute WScript.ScriptFullName, Wscript.Arguments.item(0), "", "runas", 1
WScript.quit(0)
:'batchCode
set n=n'& setlocal
set n=n'& net session >nul 2>&1
set n=n'& if not %errorlevel% == 0 (
set n=n'& if "%attemptedElevate%" NEQ "1" (
set n=n'& set attemptedElevate=1
set n=n'& echo "Elevating permissions..."
set n=n'& cscript /e:vbs %~f0 %1
set n=n'& exit /b 1 ) else (
set n=n'& echo "Elevating permissions failed" ) )
set n=n'& set PATTERN=%1
set n=n'& for /f "tokens=3*" %%I in ('"netsh interface show interface| findstr %PATTERN%"') DO (
set n=n'& echo Disabling %%J
set n=n'& netsh interface set interface name="%%J" admin=disable )
set n=n'& for /f "tokens=3*" %%I in ('"netsh interface show interface| findstr %PATTERN%"') DO (
set n=n'& echo Enabling %%J
set n=n'& netsh interface set interface name="%%J" admin=enable )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment