Go to ...

RSS Feed

Batch file to select which NIC should be used

This morning, while browsing TenForums and having my breakfast (two mugs of coffee, two cigarettes), I saw a post entitled How to configure Ethernet and wireless channels on the same computer. The OP (original poster) wants to select which NIC to use, disconnecting the other. The important thing to the OP was that when for instance the wireless NIC is connected, Ethernet would be disconnected without any need to physically disconnect the Ethernet cable.

I remembered that a few years ago, I had resolved almost the same issue using two batch files. I say almost the same, because the goal with my batch files was to select which of two wired NICs to use, as both were connected to a different router and ISP. Anyway, after minor edits, both batch files work perfectly, doing exactly what the OP asked.

Check NIC Index and Wireless Network Profile Name

Two commands are needed. First, check NIC index values with the following command (#1 in screenshot):

wmic nic get name, index

Note the Index values for wired and wireless NIC (#2 & #3 in screenshot)

Next, to be sure the correct network profile is used, check profiles (#4):

netsh wlan show profiles

Note the network profile name (#5).

Batch 1: Disconnect Ethernet, connect to wireless

Using the Index values I got for wired and wireless adapters, and the network profile name, I can now make the batch file disconnect Ethernet and connect to the wireless NIC and its associated network:

wmic path win32_networkadapter where index=2 call disable
netsh wlan connect YourNetworkProfileName
msg.exe * /time:30 Ethernet disconnected. You are now connected to wireless network

I save this batch file as WIFI.bat.

Batch 2: Disconnect wireless, connect to Ethernet

The second batch file will disconnect from the wireless network by disabling it, then re-enable it to make it ready for the next time the user runs WIFI.bat. Finally, it enables Ethernet which automatically connects that NIC to its network (subject to the Ethernet cable being connected, of course):

wmic path win32_networkadapter where index=4 call disable
wmic path win32_networkadapter where index=4 call enable
wmic path win32_networkadapter where index=2 call enable
msg.exe * /time:30 Wi-Fi disconnected. You are now connected to wired network.

I save this batch file as WIRED.bat.

Elevated shortcuts

Now, the only thing missing is to create shortcuts to run these batch files with elevated privileges:

Just in case you are not familiar with this technique, see steps 4 through 10 in this tutorial on Ten Forums: Create Elevated Command Prompt Shortcut in Windows 10. This method elevates shortcuts. Thus, the UAC prompt will be shown when these shortcuts are run.

To make elevated screenshots without a UAC prompt, see this tutorial: How to Create Elevated App Shortcut without UAC Prompt in Windows 10

That’s it. Depending on which shortcut is run, one NIC will be connected, the other disconnected. The user will get an on-screen notification indicating which NIC is currently connected. That notification will close after 30 seconds, if the user does not close it manually.



Author: Kari Finn

A former Windows Insider MVP, Kari started in computing in the mid 80’s writing code for VAX / VMS systems. Since then, he’s worked in a variety of IT positions. He specializes in Windows image capture, customization, repair and deployment as well as Hyper-V virtualization. Kari is a proud Team Member at number #1 Windows site TenForums.com.

Leave a Reply