启用/禁用以太网的批处理,用于一个网卡切换本地网络和wifi使用(Win10)

注意下面时英文版上默认网络使用,同时接入了网线和wifi时,本地网络优先wifi。

所以禁用本地网络就会自动连接到wifi,启用本地网络,就会禁用wifi。

批处理支持 -y 参数,跳过用户输入y,代码如下:

SwitchEthernet.bat

@echo off
pushd "%~dp0"
set name="Ethernet"
set findtext="Administrative state: Enabled"
set input=
set yes=0
if /i "%1"=="-y" set yes=1

"%SYSTEMROOT%system32cacls.exe" "%SYSTEMROOT%system32configSYSTEM" >nul 2>nul
if %errorlevel%==0 goto :START

echo ** Please run as administrator. **
goto :END

:START
netsh interface show interface name=%name%
netsh interface show interface name=%name% | find %findtext% > nul
if ERRORLEVEL 1 (
  set op=ENABLED
) else (
  set op=DISABLED
)
echo.
call :SwitchNetInterface
goto :END

:SwitchNetInterface
if %yes% equ 1 goto :sni_skip
set /p input=Input 'Y' to %op% interface %name% [y/N]:
goto :sni_run
:sni_skip
echo Input 'Y' to %op% interface %name% [y/N]:y
set input=Y
:sni_run
if /i "%input%"=="Y" (
  netsh interface set interface name=%name% admin=%op%
) else (
  echo cancelled.
)
goto :eof

:END
echo.
echo Current Status:
netsh interface show interface
if %yes% equ 1 goto :skip_any_key
echo Press any key to exit...
pause > nul
goto :end1
:skip_any_key
echo Will exit in 5 seconds...
ping 127.1 -n 6 > nul
:end1
popd