Help with a batch script for enabling / disabling network cards conditionally RRS feed

  • Question

  • Hello 

    I need to change network interface from Ethernet to WiFi on a regular basis, and the way I've always done this is by disabling the network card that is not in use. This helps me be 100% sure than I am working on the correct interface at all times. 

    In order to achieve this, I have 2 batch scripts bound to keyboard shortcuts. These are:

    • To go from WiFi to Ethernet:

    netsh interface set interface "WiFi" admin=disable

    netsh interface set interface "Ethernet" admin=enable

    • And then to go from Ethernet back to WiFi:

    netsh interface set interface "Ethernet" admin=disable

    netsh interface set interface "WiFi" admin=enable

    Whilst these do work, they are a bit convoluted for what I am attempting to achieve as it requires 2 separate scripts. What I would ideally like is a single batch file that does the following.

    • Checks which interface is enabled, WiFi or Ethernet
    • If WiFi is enabled, disable it and enable Ethernet
    • If Ethernet is enabled, disable it and enable WiFi

    This will allow me to stick to a single batch script which would be much tidier and save my time overall as well. The problem is conditional statements in batch are a bit outside my comfort zone and I'm not too sure how I would query the current status of the adapters either, so any assistance with this one would be very much appreciated. 

    • Moved by Bill_Stewart Monday, March 12, 2018 7:19 PM This is not "scripts on demand"
    Thursday, January 11, 2018 1:37 PM

All replies

  • $lan = Get-NetAdapter Ethernet
    $wifi = Get-NetAdapter WiFi
    if($lan.State -eq 3){
        $lan | Enable-NetAdapter
        $wifi | Disable-NetAdapter
        $lan | Disable-NetAdapter
        $wifi | Enable-NetAdapter


    Thursday, January 11, 2018 2:17 PM
  • Thanks very much for the reply jrv!

    but doesn't seem to work for me. 

    Thursday, January 11, 2018 3:28 PM
  • Requires Windows 10 and a good technical understanding of Windows.


    Thursday, January 11, 2018 3:29 PM