Solved network connections on/off toggle .bat

Asus netbook / R750jv
March 4, 2019 at 00:44:47
Specs: Windows 10, 16GB ram
Hello members
I would like to create a toggle .bat file to turn off/on the network adapters
Would someone be able to help?
I used various bits of code I have found on various place to start with...
Let's see how we go.....

netsh interface show interface "SPARK-8AUU5J" |find "Connected" >nul && (
  echo connected - disconnecting...
  netsh interface set interface "SPARK-8AUU5J" disabled
) || (
  echo disconnected - connecting
  netsh interface set interface "SPARK-8AUU5J" enabled
)
netsh interface show interface "FRITZ-Office" |find "Connected" >nul && (
  echo connected - disconnecting...
  netsh interface set interface "FRITZ-Office" disabled
) || (
  echo disconnected - connecting
  netsh interface set interface "FRITZ-Office" enabled
)
netsh interface show interface "FRITZ!Box 7490" |find "Connected" >nul && (
  echo connected - disconnecting...
  netsh interface set interface "FRITZ!Box 7490" disabled
) || (
echo disconnected - connecting
  netsh interface set interface "FRITZ!Box 7490" enabled

/*
User profiles
-------------
All User Profile     : FRITZ-Office
All User Profile     : SPARK-8AUU5J
All User Profile     : FRITZ!Box 7490
*/

message edited by Luregn


See More: network connections on/off toggle .bat

Report •

#1
March 4, 2019 at 01:40:26
i've made one before, which would toggle both wifi and cable at the same time.
give me like a hour to figure out how again :D

i5-6600K[delid]@4.7GHz/4.4GHz cache/@1.395v | 2x4GB Crucial-DDR4-2400CL17@14-15-15-28 1T 3000MHz@1.4v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2260MHz@1500MHz timings BiosModded


Report •

#2
March 4, 2019 at 01:53:45
run as admin and replace "LAN-verbinding" with any other adapter-name(CASE-sensitive)

@echo off
for /f "tokens=1" %%a in ('netsh interface show interface ^| find "LAN-verbinding"') do if "%%a"=="Enabled" (
netsh interface set interface "LAN-verbinding" Disabled
) else (
netsh interface set interface "LAN-verbinding" Enabled
)

i5-6600K[delid]@4.7GHz/4.4GHz cache/@1.395v | 2x4GB Crucial-DDR4-2400CL17@14-15-15-28 1T 3000MHz@1.4v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2260MHz@1500MHz timings BiosModded


Report •

#3
March 4, 2019 at 01:58:17
✔ Best Answer
adapter names can be found by using this command in cmd.exe:

netsh interface show interface


below code should be run as admin to toggle all 3 adapters and should work, if the the adapters are in fact called "FRITZ-Office", "SPARK-8AUU5J" and "FRITZ!Box 7490"


@echo off
for /f "tokens=1" %%a in ('netsh interface show interface ^| find "SPARK-8AUU5J"') do if "%%a"=="Enabled" (
netsh interface set interface "SPARK-8AUU5J" Disabled
) else (
netsh interface set interface "SPARK-8AUU5J" Enabled
)
for /f "tokens=1" %%a in ('netsh interface show interface ^| find "FRITZ-Office"') do if "%%a"=="Enabled" (
netsh interface set interface "FRITZ-Office" Disabled
) else (
netsh interface set interface "FRITZ-Office" Enabled
)
for /f "tokens=1" %%a in ('netsh interface show interface ^| find "FRITZ!Box 7490"') do if "%%a"=="Enabled" (
netsh interface set interface "FRITZ!Box 7490" Disabled
) else (
netsh interface set interface "FRITZ!Box 7490" Enabled
)

i5-6600K[delid]@4.7GHz/4.4GHz cache/@1.395v | 2x4GB Crucial-DDR4-2400CL17@14-15-15-28 1T 3000MHz@1.4v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2260MHz@1500MHz timings BiosModded


Report •

Related Solutions

#4
March 4, 2019 at 01:59:47
I would advise to create a shortcut to the .bat file and in the shortcut settings click the checkbox called "always run as admin" & place it on the desktop

i5-6600K[delid]@4.7GHz/4.4GHz cache/@1.395v | 2x4GB Crucial-DDR4-2400CL17@14-15-15-28 1T 3000MHz@1.4v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2260MHz@1500MHz timings BiosModded


Report •

#5
March 4, 2019 at 08:34:10
Thanks@hidde663
Sorry for the late reply. It was a late night question and I just got up a half an hour ago.
I must've used a wrong command to find the network adapters last night.
Anyway here they are:
Enabled Connected Dedicated Ethernet
Enabled Connected Dedicated WiFi
Si I have adapted your code to this:
@echo off
for /f "tokens=1" %%a in ('netsh interface show interface ^| find "Ethernet"') do if "%%a"=="Enabled" (
netsh interface set interface "Ethernet" Disabled
) else (
netsh interface set interface "Ethernet" Enabled
)
for /f "tokens=1" %%a in ('netsh interface show interface ^| find "WiFi"') do if "%%a"=="Enabled" (
netsh interface set interface "WiFi" Disabled
) else (
netsh interface set interface "WiFi" Enabled

Perfect! Thanks a million.
Now I was trying to get it to work as admin without the prompt. I've seen a few versions and tried one without any effect.
https://superuser.com/questions/117...

message edited by Luregn


Report •

#6
March 4, 2019 at 08:41:18
and if you replace %%a with %%1 in the second toggle, so like this:

@echo off
for /f "tokens=1" %%a in ('netsh interface show interface ^| find "Ethernet"') do if "%%a"=="Enabled" (
netsh interface set interface "Ethernet" Disabled
) else (
netsh interface set interface "Ethernet" Enabled
)
for /f "tokens=1" %%1 in ('netsh interface show interface ^| find "WiFi"') do if "%%1"=="Enabled" (
netsh interface set interface "WiFi" Disabled
) else (
netsh interface set interface "WiFi" Enabled

i5-6600K[delid]@4.7GHz/4.4GHz cache/@1.395v | 2x4GB Crucial-DDR4-2400CL17@14-15-15-28 1T 3000MHz@1.4v | Sapphire Nitro+ SE RX 590 8GB@1525Mhz core@1.0v/2260MHz@1500MHz timings BiosModded


Report •

#7
March 4, 2019 at 09:27:31
Don't use numbers as FOR variables. Numbers are for command line arguments. Besides, pretty sure the script's problem has more to do with the missing right parenthesis than using %%a a second time.

Untested:

SETLOCAL EnableDelayedExpansion
SET enabled=disabled
SET disabled=enabled
FOR %%A IN (Ethernet WiFi) DO CALL :Toggle %%A
EXIT /B
:Toggle
FOR /F %%a IN ('netsh interface show interface ^| find "%1"') DO netsh interface set interface %1 !%%a!

Luregn: Now I was trying to get it to work as admin without the prompt.
That goes directly against UAC's design. If you want to avoid the UAC prompt, disable UAC.

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

#8
March 4, 2019 at 09:33:24
Thanks @Razor2.3
Hmmm... and how would the entire command look like(for a close to newbie in coding)?

Report •

#9
March 4, 2019 at 09:46:45
For what? Disabling UAC? There's no command to do so. You could change the registry key that controls UAC and reboot, but at that point you might as well just use the GUI.

How To Ask Questions The Smart Way


Report •

#10
March 4, 2019 at 10:08:00
No, not the UAC. I've given up on that.
What I meant was this command, assuming it is only part of it?
SETLOCAL EnableDelayedExpansion
SET enabled=disabled
SET disabled=enabled
FOR %%A IN (Ethernet WiFi) DO CALL :Toggle %%A
EXIT /B
:Toggle
FOR /F %%a IN ('netsh interface show interface ^| find "%1"') DO netsh interface set interface %1 !%%a!


Report •

#11
March 4, 2019 at 10:43:39
No, that's pretty much it. My batch is rusty and the script's untested (I typed it from scratch directly into this thread), but I've seen plenty of such scripts back in the day. So long as "Ethernet" and "WiFi" are correct and complete, including capitalization, I don't see any obvious problem. Or you can continue to use your modified hidde663 script, so long as you restore the missing right parenthesis at the end.

How To Ask Questions The Smart Way


Report •

#12
March 4, 2019 at 10:45:56
Thanks a lot, that should do for now.

Report •

Ask Question