Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Find Network Adapter name and store this name inside a variable  (Read 15162 times)

0 Members and 1 Guest are viewing this topic.

Abo-Zead

    Topic Starter


    Beginner
  • Thanked: 1
  • Experience: Familiar
  • OS: Windows 10
Hi all
after some research for a command that can get NIC name for windows OS for example "Ethernet" or "Local Area Connection" etc
I found that command and it works properly but how we can store the output value "I mean the name of NIC" inside a variable.

here is that command
Code: [Select]
wmic nic get NetConnectionID

Hackoo



    Hopeful
  • Thanked: 42
  • Experience: Expert
  • OS: Windows 10
Re: Find Network Adapter name and store this name inside a variable
« Reply #1 on: April 20, 2021, 01:18:53 AM »
Hi  ;)
Refer to this : Pass netconnectionid to a netsh interface command in batch
Code: [Select]
@echo off
:: tokens=2 indicates that of all the tokens that would normally be returned, we only want the second one
:: delims== indicates that the string should be split up wherever there is an = symbol
for /f "tokens=2 delims==" %%A in ('wmic nic where "netconnectionid like '%%'" get netconnectionid /value') do (
    call :dhcp "%%A"
)
pause
exit /b

:dhcp
echo %1
:: %~1 refers to the first parameter passed into the function, but without the quotation marks
::netsh interface set address %1 dhcp

We can store and set all variables into an array like this code :

Code: [Select]
@echo off
Title Find Network Adapter name and store this name inside a variable
SetLocal EnableDelayedExpansion
@for /f "tokens=2 delims==" %%A in ('wmic nic where "netconnectionid like '%%'" get netconnectionid /value') do (
@for /f "delims=" %%B in ("%%A") do (
Set /A Count+=1
Set "NET_CONN_ID[!Count!]=%%B"
)
)

@for /L %%i in (1,1,%Count%) do (
echo NetConnectionId%%i = "!NET_CONN_ID[%%i]!"
)
pause

As a result, you can got some thing like me :

Code: [Select]
NetConnectionId1 = "Ethernet"
NetConnectionId2 = "Wi-Fi"
NetConnectionId3 = "Connexion réseau Bluetooth"
« Last Edit: April 20, 2021, 01:41:05 AM by Hackoo »

Abo-Zead

    Topic Starter


    Beginner
  • Thanked: 1
  • Experience: Familiar
  • OS: Windows 10
Re: Find Network Adapter name and store this name inside a variable
« Reply #2 on: April 20, 2021, 08:31:02 PM »
Thanks, Hackoo for your usual help but can we enable these detected Network cards also for example by using the below command.
Code: [Select]
netsh interface set interface name="Name of detected network" admin=ENABLED>nul

as I tried to use this command but it doesn't work for me, so could you help.

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Find Network Adapter name and store this name inside a variable
« Reply #3 on: April 21, 2021, 07:24:24 AM »
All the examples I see on the web say this is proper syntax

Code: [Select]
netsh interface set interface "Ethernet" disable
netsh interface set interface "Wi-Fi" enable

Abo-Zead

    Topic Starter


    Beginner
  • Thanked: 1
  • Experience: Familiar
  • OS: Windows 10
Re: Find Network Adapter name and store this name inside a variable
« Reply #4 on: April 22, 2021, 12:06:45 AM »
All the examples I see on the web say this is proper syntax

Code: [Select]
netsh interface set interface "Ethernet" disable
netsh interface set interface "Wi-Fi" enable

Thanks, Squashman Your opinion is correct but also I saw the previous code on the web, kindly look into this link As proof
https://stackoverflow.com/questions/19831023/enable-disable-network-connection-from-command-line.
, so I'm trying to merge between two this command and Hackoo array