Subscribe

Get the Network Administrators tool pack

Subscribe to our newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them.

Click Here to get your free tools

Recent Posts

Search

Archives

Automatically disable wireless when Ethernet is available

Post image for Automatically disable wireless when Ethernet is available

by Steve Wiseman on January 11, 2012 · 69 comments

in Scripts,Tips,Tools


.

Got a question from Enrico,

“Good morning Steve, and Happy New Year!…I would like that when the user has a wired NIC up and running, the wireless NIC is automatically disabled, and as soon as the wired NIC disconnects, the wireless NIC activates.

This prevents the user to have both NICs up and running at the same time and above all prevents wireless access points to collapse for too many unneeded simultaneous connections.”

Awesome question Enrico.

There are other reasons for wanting this too. If you have both connections running, things like file transfers, or anything that needs the extra speed of Ethernet…might end up going through the wireless instead.

For this question, I have created a VB Script that will receive the names of the two network cards in its command line parameters.

For example, if you look at your network connections:

Network Connections

See the descriptions for them? That is what you will use to identify them.

If you wanted to watch the “Local Area Connection” adapter…and then switch the “Wireless Connection” Adapter, you would call the script like this:

Watch Ethernet Example

The program will not display anything, but run in the background looking for the change. It will continue to run until the user logs out.

If the Local Area Connection is no longer connected, it will automatically enable the Wireless connection.

The reverse is true…if Local Area Connection becomes available, it will automatically disable the wireless connection.

The best place to run the script would be from a group policy computer configuration logon script (Not a user configuration) because it will run as local system and have the proper permission to enable and disable network cards.

You can get the script from here:

For 2003, Vista, Windows 7, and 2008:
NetSwitch.dat

For Windows XP:

NetSwitchXP.dat

Just rename to NetSwitch.vbs after downloading.

One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools

Related Articles:

{ 19 comments… read them below or add one }

1 Chris DuPlantis August 20, 2013 at 11:02 am

Awesome script Steve.

to Remco’s question. How would you modify the script to release the DHCP address prior to switching adapters?

On Error Resume Next

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)

Set colNetCards = objWMIService.ExecQuery _
(“Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True”)

For Each objNetCard in colNetCards
objNetCard.ReleaseDHCPLease()
Next

This manages to release the address, not sure how to insert into your code.

any help would be great! Thanks

2 Steve Wiseman August 20, 2013 at 3:08 pm

Hi Chris,

Just saw it on the website.

You could convert that script to a function like this:

Sub RefreshDHCP()
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNetCards = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

For Each objNetCard in colNetCards
objNetCard.ReleaseDHCPLease()
Next
End Sub

Then, inside the EnableAdapter() sub (Right before End Sub)

You could call it like this:

RefreshDHCP()

Hope that helps,

Steve

3 Titus November 1, 2013 at 4:48 am

Hi Steve, thanks a lot!

I have been looking for this for a very long time. I set the script to run once login by creating a batch file and put it to the startup folder.

I also saw the coding from Chris and yourself on the releasing of DHCP which will help alot as one pc using 2 ip address is too much and I always have to clear the DHCP scope manually. I’m not much into coding so I will greatly appreciate if you can incorporate the script to release DHCP into your original script. Thanks again!

4 Zola November 18, 2013 at 5:43 am

Do you have a script to actually change the registry bind order for the NICs

5 Zola November 18, 2013 at 5:49 am

I need a permanent registry reorder of the NICs. Any VB Scripts available?

6 Steve Wiseman November 22, 2013 at 2:03 pm

Hi Zola,

As far as I know…I have not seen any. The issue would be that I believe this is different in different versions of Windows. Trying to write this to the registry could corrupt your entire network setup if not done just right.

Not saying it is not possible…just that one would need to be very careful…and I don’t think my hands are steady enough for this one 🙂

7 Haydar December 20, 2013 at 8:08 am

Nice script, but it works only one way.

When I attach the network cable to the Laptop it DISABLES the Wireless, but when I unplug the network cable it should DISABLE the LAN Adabter and ENABLE the wireless. I know it depends, which card you use to watch, but I want use the both way. Is there any chance to achieve this?

Thanks

Haydar

8 Steve Wiseman December 20, 2013 at 11:25 am

Hi Haydar,

I believe this is an issue with the windows subsystem. VB Script tries to disable the card, but returns an error because no cable is connected to the device.

Thanks,

Steve

9 Haydar December 20, 2013 at 12:27 pm

is there a way to solve it???

10 Steve Wiseman December 20, 2013 at 2:36 pm

Hello Haydar,

I never like to say never, but from VB Script I don’t know if it is possible. There might be a way if someone developed an application that dug into the windows api

11 Tony January 14, 2014 at 5:19 am

Is there a license to go with this script or any other conditions around commercial use? interested to use on our site here (staff systems only not customers) and need to be sure that there is no catch in doing so 🙂

12 Q February 18, 2014 at 10:51 am

For those who don’t mind or can use a program to get this functionality, Lenovo Access Connections actually does a pretty good job of this. It does work with other laptop manufacturers (I know it works with HP ProBooks and Dell Precisions)

13 Tim S March 4, 2014 at 4:53 pm

Hi, Steve — thanks for this script, this goes a long way to solving my connections issues. Not being a VBScripter myself, is there a way to alter the script so that multiple connections would be disabled at the same time as the Wireless? IE, I’d like to have it disable the ‘Mobile Broadband Connection’ and possible another as well as the ‘Wireless Network Connection’ when the LAN is plugged in. Any assistance would be greatly appreciated — thanks again!

14 Ryan March 5, 2014 at 6:01 pm

Steve:

This script is excellent. I’ve implemented this in my organization via GPO, but there’s one catch: It does not work for non-admins.

I apologize for my lack of GPO knowledge, but where exactly should this script be configured in a GPO so that it works for non-admins? Currently I have it configured under User Configuration -> Windows Settings -> Scripts (Logon/Logoff) -> Logon. The parameters are set to “Local Area Connection” “Wireless Network Connection”.

The only other place I can find under Computer Configuration is: Computer Configuration -> Windows Settings -> Scripts (Startup/Shutdown) -> Startup … but it doesn’t seem to work there.

Any help would be great! I’d love to push this out to all of my users.

We are running Server 2003 and Windows 7 x64 client laptops.

15 Steve Wiseman March 7, 2014 at 12:17 am

Hi Ryan,

Not much you can do with non admins. The reason is that you need to have local admin rights to enable or disable a network card.

The alternative would be to have a service that sits in the background that watches the network interfaces and does the work for you…but I have not seen such an app out there yet

Steve

16 Rajini May 27, 2014 at 4:21 am

Hi ..

Yes, if you make it as server which will run from System account and watch network behavior.
it will be great.

Pls let me know if you make it

Regards
S. Rajini

17 Wilson January 26, 2015 at 10:21 am

good morning
I would like your help … Now download the script, change the file extension, can not get the script to work me. I am connected by ethernet and wifi does not turn off, the operating system language and script has something to do, my system is in Spanish.

18 Johan Carlsson March 3, 2015 at 9:07 am
19 Ronan March 30, 2015 at 7:55 am

Hi All,

This has really saved me a lot of time – thanks for all of your help.

I am however having one issue. While the script works and works well I have added in the feature to release the Wireless SHCP lease once disabled but once I connect back in my LAN cable it’s coming up as “unidentiifed network” and the only way to get it back in action is to ipconfig /renew – where am I going worng?

‘*********************************************
‘* Net Switcher – Watches one network card *
‘* for connectivity, and toggles another *
‘* http://www.intelliadmin.com *
‘*********************************************

Sub EnableAdapter( sAdapterName, bStatus )
Dim objWMIService, colItems
Set objWMIService = GetObject(“winmgmts:\\.\root\cimv2”)
Set colItems = objWMIService.ExecQuery(“Select * from Win32_NetworkAdapter”,,48)
For Each objItem in colItems
if (UCase(Trim(objItem.NetConnectionID))=UCase(Trim(sAdapterName))) then
if (bStatus) then
objItem.Enable
else
objItem.Disable
end if
end if
Next
RefreshDHCP()

End Sub

Sub RefreshDHCP()

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colNetCards = objWMIService.ExecQuery(“Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True”)

For Each objNetCard in colNetCards
objNetCard.ReleaseDHCPLease()
Next
End Sub

Function AdapterStatus( sAdapterName )
Dim objWMIService, colItems
Set objWMIService = GetObject(“winmgmts:\\.\root\cimv2”)
Set colItems = objWMIService.ExecQuery(“Select * from Win32_NetworkAdapter”,,48)

AdapterStatus = false
For Each objItem in colItems
if (UCase(Trim(objItem.NetConnectionID))=UCase(Trim(sAdapterName))) then
AdapterStatus = (objItem.NetConnectionStatus=2)
end if
Next
End Function

Function AdapterExists( sAdapterName )
Dim objWMIService, colItems
Set objWMIService = GetObject(“winmgmts:\\.\root\cimv2”)
Set colItems = objWMIService.ExecQuery(“Select * from Win32_NetworkAdapter”,,48)

AdapterExists = false
For Each objItem in colItems
if (UCase(Trim(objItem.NetConnectionID))=UCase(Trim(sAdapterName))) then
AdapterExists = True
end if
Next
End Function

Dim bCurrentStatus
Dim bChanged
Dim sWatchNetworkCard
Dim sSwitchNetworkCard

if (WScript.Arguments.Count<2) then
WScript.Echo "*********************************"
WScript.Echo "* IntelliAdmin Net Switcher *"
WScript.Echo "* http://www.intelliadmin.com *"
WScript.Echo "*********************************"
WScript.Echo vbCrLf & "Usage: " & vbCRLF
WScript.Echo " NetSwitch.vbs ”
WScript.Echo vbCrLF & “Explanation: ” & vbCRLF
WScript.Echo ” Net Switcher can be used to make sure your”
WScript.Echo ” wireless card is only enabled when no ”
WScript.Echo ” ethernet connection is available”
WScript.Echo vbCrLF & “Example: ” & vbCRLF
WScript.Echo ” NetSwitch.vbs ” & chr(34) & “Local Area Connection” & chr(34) & ” ” & _
chr(34) & “Wireless Connection” & Chr(34)
WScript.Quit

end if

sWatchNetworkCard = WScript.Arguments(0)
sSwitchNetworkCard = WScript.Arguments(1)

if (Not(AdapterExists(sWatchNetworkCard))) then
WScript.Echo “Error: Could not find the adapter (” & sWatchNetworkCard & “)”
WScript.Quit
end if

if (Not(AdapterExists(sSwitchNetworkCard))) then
WScript.Echo “Error: Could not find the adapter (” & sSwitchNetworkCard & “)”
WScript.Quit
end if

bChanged=TRUE

while(True)
if (bChanged) then
bChanged=FALSE
bCurrentStatus = AdapterStatus(sWatchNetworkCard)
if (bCurrentStatus) then
EnableAdapter sSwitchNetworkCard,False
else
EnableAdapter sSwitchNetworkCard,True
end if
end if
WScript.Sleep(1000)
if (bCurrentStatusAdapterStatus(sWatchNetworkCard)) then
bChanged=TRUE
end if
wend

Leave a Comment

Category Links - Windows Forum - Exchange Forum