Page 1 of 1

netsh Interface name store as variable

Posted: 13 Oct 2023 02:17
by shokarta
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?

Re: netsh Interface name store as variable

Posted: 14 Oct 2023 05:11
by Batcher
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

Re: netsh Interface name store as variable

Posted: 02 Apr 2024 01:11
by shokarta
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

Re: netsh Interface name store as variable

Posted: 02 Apr 2024 08:10
by ShadowThief
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%