netsh Interface name store as variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
shokarta
Posts: 16
Joined: 14 Oct 2012 05:54

netsh Interface name store as variable

#1 Post by shokarta » 13 Oct 2023 02:17

hello guys,

so netsh interface show interface gives me this:

Code: Select all

Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled        Disconnected   Dedicated        Wi-Fi
Enabled        Connected      Dedicated        Ethernet 2
Enabled        Disconnected   Dedicated        Ethernet
and I am looking for a way to store the Interface Name which has state connected (in this example it wold be "Ethernet 2" as variable,
so later on I can do soemthing like this:

Code: Select all

@NETSH interface set interface "Ethernet 2" DISABLED
@NETSH interface set interface "Ethernet 2" ENABLED
but instead hardcoded interface name, i would like to use the one from variable set above...

how can I do that?

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: netsh Interface name store as variable

#2 Post by Batcher » 14 Oct 2023 05:11

test-1.bat

Code: Select all

@echo off
for /f "tokens=3*" %%a in ('netsh interface show interface ^| findstr "Connected"') do (
    set "AdapterName=%%b"
)
echo,%AdapterName%
pause

shokarta
Posts: 16
Joined: 14 Oct 2012 05:54

Re: netsh Interface name store as variable

#3 Post by shokarta » 02 Apr 2024 01:11

Hello,

can I kindly ask for one more modification?

So from this, basicaly I see in my interfaces:

Code: Select all

Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled        Disconnected   Dedicated        Wi-Fi
Enabled        Connected      Dedicated        Ethernet 2
Enabled        Connected      Dedicated        VMware Network Adapter VMnet1
Enabled        Connected      Dedicated        VMware Network Adapter VMnet8
Enabled        Disconnected   Dedicated        Ethernet
and while it loops it sets the Ethernet 2, then it replaces vy VMnet1, and after all it replaces by VMnet8 as it looks for ANY "CONNECTED" state...
however, I am realy looking for any "Ethernet" network only...

so can I kindly ask to modify the code bellow to only look for Ethernet* networks only?

Thank you

ShadowThief
Expert
Posts: 1162
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: netsh Interface name store as variable

#4 Post by ShadowThief » 02 Apr 2024 08:10

Pipe a second findstr for "Ethernet"

Code: Select all

@echo off
for /f "tokens=3*" %%a in ('netsh interface show interface ^| findstr "Connected" ^| findstr "Ethernet"') do (
    set "AdapterName=%%b"
)
echo %AdapterName%

Post Reply