What you are looking for is the for /F
loop, which is capable of capturing the output of a command:
for /F "tokens=1* delims==" %%J in ('
wmic NIC where "NetConnectionStatus=2" get NetConnectionID /VALUE
') do (
for /F "delims=" %%I in ("%%K") do (
netsh interface set interface name="%%I" newname="LAN"
)
)
The inner for /F
loop here is needed to avoid artefacts (like orphaned carriage-return characters) from the conversion of wmic
's Unicode text to ASCII/ANSI text by the outer for /F
(see also this answer of mine).
I also changed the output format of wmic
by the /VALUE
option as recommended by user Compo in a comment, which avoids trouble with potential trailing SPACEs.
Note that the wmic
query might return more than one adapter, so perhaps you want to extend the where
clause to avoid that, like this, for example:
for /F "tokens=1* delims==" %%J in ('
wmic NIC where "NetConnectionStatus=2 and NetConnectionID like '%%Ethernet%%'" get NetConnectionID /VALUE
') do (
for /F "delims=" %%I in ("%%K") do (
netsh interface set interface name="%%I" newname="LAN"
)
)
The %%
in the batch file represents one literal %
, and a %
is a wildcard in where
clauses, so the above code only returns items with Ethernet
in their names (in a case-insensitive manner).
To guarantee that only one item is touched by netsh
, you could simply use goto
in the for /F
loops to break them:
for /F "tokens=1* delims==" %%J in ('
wmic NIC where "NetConnectionStatus=2" get NetConnectionID /VALUE
') do (
for /F "delims=" %%I in ("%%K") do (
netsh interface set interface name="%%I" newname="LAN"
goto :NEXT
)
)
:NEXT
Reference: Win32_NetworkAdapter class