Managing Network Connections via Windows Command Line

This article explores how to enable and disable network adapters using Windows command-line scripts, addressing limitatinos of older tools and providing incremental improvements.

Initial Version: Enabling Disconnected Adapters

The fundamental command for managing network interfaces is netsh interface set interface. To re-enable all interfaces currently in a disconnected state, we can parse the output of netsh interface show interface and filter for lines containing "Disconnected".


@echo off
for /f "tokens=*" %%a in ('netsh interface show interface ^| find "Disconnected"') do (
    netsh interface set interface "%%a" admin=enable
)

The core logic here is netsh interface set interface "%%a" admin=enable, which activates the network adapter named %%a. The netsh interface show interface command lists all available adapters, and the pipe | find "Disconnected" filters this list to identify those that need to be enabled.

Intermediate Version: Excluding Specific Adapters

When working remotely, it's crucial to avoid disconnecting your active session. This version introduces a mechanism to skip adapters based on a keyword in their name.


@echo off
setlocal enabledelayedexpansion

rem Define keywords for adapters to skip
set "skipKeyword=RemoteAccess"

rem List all network adapters
for /f "tokens=*" %%a in ('netsh interface show interface ^| find "Connected"') do (
set "interfaceName=%%a"
rem Check if the adapter name contains the skip keyword
echo !interfaceName! | find /i "%skipKeyword%" >nul
if errorlevel 1 (
rem If the keyword is not found, disable the adapter
netsh interface set interface "!interfaceName!" admin=disable
) else (
echo Skipping adapter: !interfaceName!
)
)
endlocal

This script utilizes echo !interfaceName! ^| find /i "%skipKeyword%" >nul. The find /i command searches case-insensitively for the specified keyword. If the keyword is not found, errorlevel is set to 1. The >nul suppresses any output from the find command.

Advanced Version: Targeted Disabling by Name

In the previous versions, the entire line of output from netsh interface show interface was treated as a single string. However, the output contains multiple fields (status, state, type, and name). To precisely target an adapter by its name, we need to parse these fields individually.


@echo off
setlocal enabledelayedexpansion

rem Define the specific adapter name to disable
set "targetAdapter=VirtualBox Host-Only Network"

rem List all network adapters
for /f "tokens=1,2,3,* delims= " %%a in ('netsh interface show interface ^| findstr /r /c:"Connected"') do (
    set "status=%%a"
    set "state=%%b"
    set "type=%%c"
    set "interfaceName=%%d"
    rem Enable delayed variable expansion
    setlocal enabledelayedexpansion
    rem Check if the adapter name matches the target
    if /i "!interfaceName!"=="%targetAdapter%" (
        echo Disabled adapter: !interfaceName!
        netsh interface set interface name="!interfaceName!" admin=disable
    )
    endlocal
)
endlocal

This refined approach uses multiple variables (%%a, %%b, %%c, %%d) to capture the distinct fields from the command output:


set "status=%%a"
set "state=%%b"
set "type=%%c"
set "interfaceName=%%d"

This allows for accurate identification and manipulation of network adapters by their exact names.

The next steps involve enhancing this functionality with command-line arguments for dynamic configuration of adapter names and desired states (enable/disable), making the script more versatile.

Note: Network adapter names can be in English or other languages.

Tags: Batch Scripting netsh windows command line network management

Posted on Fri, 06 Nov 2026 16:52:01 +0000 by phpwolf