CMD/Batch get active interface name as variable

夙愿已清 提交于 2019-12-02 06:55:01

to get the names of all interfaces that are connected:

FOR /F "tokens=3,*" %%A IN ('netsh interface show interface^|find "Connected"') DO echo %%B

Note: this is language dependent.

For a language independent solution use wmic (which has it's own traps and oddities):

for /f "tokens=2 delims==" %%a in ('wmic nic where (NetConnectionStatus^=2^) get name /value') do (
  for /f "delims=" %%b in ("%%a") do echo %%b
)

The inner for is to handle the ugly wmic line endings

Because the output of WMIC has the 'ugly' <CR><CR><LF> line endings and the value of NetConnectionID will often have unwanted trailing spaces, I'd suggest something more like this to retrieve only connected network adapters as variables:

@Echo Off
Set "i=0"
For /F "Skip=1Delims=" %%A In (
 'WMIC NIC Where "NetConnectionStatus=2" Get NetConnectionID'
) Do For /F "Delims=" %%B In ("%%A") Do Call :Sub %%B
Set NIC[
Timeout -1
Exit/B
    :Sub
    Set/A i+=1
    Set "NIC[%i%]=%*"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!