Toggle enable/disable Ethernet adapter from CMD

2019-08-19 02:08发布

问题:

I'm trying to create a .bat file which can enable/disable my ethernet adapter, but I don't have much knowledge about coding or the cmd syntax. I was thinking about using the netsh command in something like:

IF " ~Ethernet adapter is enabled~ " GOTO :disable ELSE GOTO :enable

:disable
    netsh interface set interface "Ethernet" disabled

:enable
    netsh interface set interface "Ethernet" enabled

How can I do it right?

回答1:

If you are already familiar with the netsh interface command, why don't you use it?

netsh interface show interface "Ethernet" |find "Connected" >nul && (
  echo connected - disconnecting...
  netsh interface set interface "Ethernet" disabled
) || (
  echo disconnected - connecting
  netsh interface set interface "Ethernet" enabled
)


回答2:

In above solution it disconnects and connects the internet connection so I improvised like this to Toggle enable and disable Ethernet adapter and this is working perfectly for me. This code disables adapter if it is enabled and enables if it is disabled.

netsh interface show interface "Ethernet" |find "Disabled" >nul && (
  echo disabled - enabling...
  netsh interface set interface "Ethernet" enabled
) || (
  echo enabled - disabling
  netsh interface set interface "Ethernet" disabled
)