Reputation: 11
Long time reading. First time poster.
I have a Windows machine with many NICs, and rather than the normal ipconfig, I want a simple way of showing the IP address(es) of just the adapter(s) that is(are) connected.
Looking at previous questions on this forum, I think the following command would work, if somebody please point me on how to pass the double quotes to the last variable:
for /f "tokens=2 delims==" %F in ('wmic nic where "NetConnectionStatus=2 and AdapterTypeId=0" get NetConnectionID /format:list') do netsh interface ip show config name="%F%"
Note: in a batch file, it would be %%F instead of %F.
I've tried several escaping methods, but it seems the part after the %F is not recognized.
The expected output would it be something like (please ignore the Spanish, the IP address is the information of interest):
netsh interface ipv4 show config name="Wi-Fi"
Configuración para la interfaz "Wi-Fi"
DHCP habilitado: Sí
Dirección IP: 192.168.1.12
Upvotes: 1
Views: 665
Reputation: 38708
If you wanted to continue to use WMIC with NetSh, then the following may suit your purposes:
Directly in cmd.exe:
For /F Tokens^=6^ Delims^=^" %G In ('%SystemRoot%\System32\wbem\WMIC.exe /NameSpace:\\Root\StandardCimv2 Path MSFT_NetConnectionProfile Get InterfaceAlias /Format:MOF 2^>NUL') Do @For /F "Tokens=2 Delims=:" %H In ('%SystemRoot%\System32\netsh.exe Interface IPv4 Show Config Name^="%G" 2^>NUL ^| %SystemRoot%\System32\find.exe "IP"') Do @For /F %I In ("%H") Do @Echo %I
In a batch file:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
/NameSpace:\\Root\StandardCimv2 Path MSFT_NetConnectionProfile Get
InterfaceAlias /Format:MOF 2^>NUL') Do For /F "Tokens=2 Delims=:" %%H In ('
%SystemRoot%\System32\netsh.exe Interface IPv4 Show Config Name^="%%G" 2^>NUL
^| %SystemRoot%\System32\find.exe "IP"') Do For /F %%I In ("%%H") Do Echo %%I
Pause
EndLocal
Please note that this assumes that the string IP
is unique in the output for the target line, (it clearly is for both English and your language, but that is not a guarantee).
For the reason given in the footnote for the above code examples, my preference would be to just use WMIC, like this:
Directly in cmd.exe:
For /F Tokens^=2^ Delims^=^" %G In ('%SystemRoot%\System32\wbem\WMIC.exe NIC Where "Not NetConnectionStatus Is Null" Assoc /AssocClass:Win32_NetworkAdapterConfiguration 2^>NUL') Do @For /F Tokens^=2^ Delims^=^" %H In ('%SystemRoot%\System32\wbem\WMIC.exe NICConfig Where Index^=%G Get IPAddress') Do @Echo %H
In a batch file:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F Tokens^=2^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe NIC
Where "Not NetConnectionStatus Is Null" Assoc
/AssocClass:Win32_NetworkAdapterConfiguration 2^>NUL'
) Do For /F Tokens^=2^ Delims^=^" %%H In ('%SystemRoot%\System32\wbem\WMIC.exe
NICConfig Where Index^=%%G Get IPAddress') Do Echo %%H
Pause
EndLocal
I suppose you could also get the help of PowerShell too…
Directly in cmd.exe:
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias $(Get-NetConnectionProfile -IPv4Connectivity 'Subnet','LocalNetwork','Internet' -ErrorAction SilentlyContinue).InterfaceAlias | Select-Object InterfaceAlias, IPAddress"
As a batch file:
@Echo Off
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command^
"Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias "^
"$(Get-NetConnectionProfile -IPv4Connectivity 'Subnet','LocalNetwork',"^
"'Internet' -ErrorAction SilentlyContinue).InterfaceAlias | Select-Object "^
"InterfaceAlias, IPAddress"
Pause
Please note the above examples are untested.
Upvotes: -1
Reputation: 18847
Here is another solution using the two Powershell cmdlets Get-NetAdapter
and Get-NetIPAddress
with a batch file :
@echo off
setlocal enabledelayedexpansion
REM Get the list of network interfaces
for /f "tokens=*" %%a in ('powershell -Command "Get-NetAdapter | Where-Object { $_.Status -eq 'Up' } | Select-Object -ExpandProperty Name"') do (
set interface=%%a
REM Get the IP address for the current interface
for /f "tokens=*" %%b in ('powershell -Command "Get-NetIPAddress -InterfaceAlias '%%a'| Where-Object { $_.AddressFamily -eq 'IPv4' -and $_.PrefixOrigin -eq 'Dhcp' -and $_.AddressState -eq 'Preferred' } | Select-Object -ExpandProperty IPAddress"') do (
set ip=%%b
REM Check if the IP address is not empty
if not "!ip!"=="" (
echo Interface: !interface!
echo IP Address: !ip!
echo.
)
)
)
endlocal
pause
Upvotes: 0