Get only ethernet MAC-address via command prompt - windows

I use 'ipconfig /all' or 'getmac /v' to get all NIC physical addresses.
But the problem is, generally a computer has more than one NIC card. Also, there are some virtual MAC addresses like Microsoft virtual wifi hotspot NIC which shows only when wifi hotspot is on.
So, how can I collect only the address corresponding to ethernet via cmd?

This command fetches MAC addresses of physical ethernet network devices:
wmic path Win32_NetworkAdapter where "PNPDeviceID like '%PCI%' AND AdapterTypeID='0'" get name, MacAddress
if you want to fetch virtual ones as well then use this:
wmic path Win32_NetworkAdapter where "AdapterTypeID='0'" get name, MacAddress
or this one for fetching all the MAC addresses of all network devices:
wmic path Win32_NetworkAdapter get name, MacAddress
Just in case you are interested how it works:
We are using WMIC tool provided by Windows to fetch adapter info from Win32_NetworkAdapter.
Then filter ethernet adapters by AdapterTypeID=0 as 0 corresponds to ethernet type.
Finally filter physical devices with PNPDeviceID like '%PCI% since they are attached to PCI.
You might also want to look at the result of whole Win32_NetworkAdapter and play with it with this command:
wmic path Win32_NetworkAdapter

I advise to you use powershell because very powerful than cmd
Get-CimInstance win32_networkadapterconfiguration | select description, macaddress | where {$_.MACAddress -ne $n
ull }
output :
description macaddress
----------- ----------
RAS Async Adapter 20:41:53:59:4E:FF
Realtek PCIe GBE Family Controller 18:03:73:65:64:AB
VMware Virtual Ethernet Adapter for VMnet1 00:50:56:C0:00:01
VMware Virtual Ethernet Adapter for VMnet8 00:50:56:C0:00:08
that command in powershell select all macaddress for device enable your machine that included vmware but we can do more filter for example
Get-CimInstance win32_networkadapterconfiguration | select description, macaddress | where {$_.MACAddress -ne $n
ull } | where {$_.Description -match "Realtek" }
output:
description macaddress
----------- ----------
Realtek PCIe GBE Family Controller 18:03:73:65:64:AB
but if your just run in cmd you should encode this command in powershell like this
$script={ Get-CimInstance win32_networkadapterconfiguration | select description, macaddress | where {$_.MACAddr
ess -ne $null } | where {$_.Description -match "Realtek" } }
[System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes( $script))
output encoded command for use in cmd
IABHAGUAdAAtAEMAaQBtAEkAbgBzAHQAYQBuAGMAZQAgAHcAaQBuADMAMgBfAG4AZQB0AHcAbwByAGsAYQBkAGEAcAB0AGUAcgBjAG8AbgBmAGkAZwB1AHI
AYQB0AGkAbwBuACAAfAAgAHMAZQBsAGUAYwB0ACAAZABlAHMAYwByAGkAcAB0AGkAbwBuACwAIABtAGEAYwBhAGQAZAByAGUAcwBzACAAfAAgAHcAaABlAH
IAZQAgAHsAJABfAC4ATQBBAEMAQQBkAGQAcgBlAHMAcwAgAC0AbgBlACAAJABuAHUAbABsACAAfQAgACAAfAAgAHcAaABlAHIAZQAgAHsAJABfAC4ARABlA
HMAYwByAGkAcAB0AGkAbwBuACAALQBtAGEAdABjAGgAIAAiAFIAZQBhAGwAdABlAGsAIgAgAH0AIAA=
in cmd i use this and get mac
powershell -encodedcommand IABHAGUAdAAtAEMAaQBtAEkAbgBzAHQAYQBuA
GMAZQAgAHcAaQBuADMAMgBfAG4AZQB0AHcAbwByAGsAYQBkAGEAcAB0AGUAcgBjAG8AbgBmAGkAZwB1A
HIAYQB0AGkAbwBuACAAfAAgAHMAZQBsAGUAYwB0ACAAZABlAHMAYwByAGkAcAB0AGkAbwBuACwAIABtA
GEAYwBhAGQAZAByAGUAcwBzACAAfAAgAHcAaABlAHIAZQAgAHsAJABfAC4ATQBBAEMAQQBkAGQAcgBlA
HMAcwAgAC0AbgBlACAAJABuAHUAbABsACAAfQAgACAAfAAgAHcAaABlAHIAZQAgAHsAJABfAC4ARABlA
HMAYwByAGkAcAB0AGkAbwBuACAALQBtAGEAdABjAGgAIAAiAFIAZQBhAGwAdABlAGsAIgAgAH0AIAA=
output:
description macaddress
----------- ----------
Realtek PCIe GBE Family Controller 18:03:73:65:64:AB

#ECHO OFF
SETLOCAL enabledelayedexpansion
FOR /f "delims=" %%a IN ('getmac /v ^|find /i "local area conn" ') DO (
FOR %%b IN (%%a) DO (
SET element=%%b
IF "!element:~2,1!!element:~5,1!!element:~8,1!"=="---" set mac=%%b
)
)
ECHO found %mac%
GOTO :EOF
This should provide the information required, but it would have been better had you provided sample output from your machine as your experience may not be exactly reproduced on others' installations.

on commandline:
for /f %i in ('wmic nic get MACAddress ^|find ":"') do #echo %i
or
for /f %i in ('getmac^|find "-"') do #echo %i
for use in a batchfile, use %%i instead of %i

To only get the active network card's MAC-address using Powershell:
(Get-WmiObject win32_networkadapterconfiguration -ComputerName $env:COMPUTERNAME | Where{$_.IpEnabled -Match "True"} | Select-Object -Expand macaddress) -join ","

You can try this :
netsh interface ip show addresses "Ethernet"

Related

Is there any command to display active Ethernet connection name?

I need to display connected Ethernet name.
I used the following command to get WLAN connected network (Wi-Fi).
C:\Users\User>netsh wlan show interface | findstr /i "SSID" name
SSID : HOME
BSSID : bc:62:d2:4a:a1:48
An error message is output if I run the following command next:
C:\Users\User>netsh lan show interface command
The Wired AutoConfig Service (dot3svc) is not running.
But after I started the service, I still do not get the active connected Ethernet name.
C:\Users\User>netsh lan show interface command
There is 1 interface on the system:
Name : Ethernet
Description : Realtek PCIe GbE Family Controller
GUID : e901d916-86f4-4070-9941-47a9a325537a
Physical Address : 98-E7-43-0F-8F-84
State : Connected. Network does not support authentication.
Can anyone help me to get the Ethernet name Home on wired LAN connection?
The following command, entered directly in cmd, should display the name of the connected network profile(s), regardless of whether they are wired or wireless, on a Windows 8 / Server 2012 Operating System or newer.
For /F Tokens^=6^ Delims^=^" %G In ('%SystemRoot%\System32\wbem\WMIC.exe /NameSpace:\\Root\StandardCimv2 Path MSFT_NetConnectionProfile Where "IPv4Connectivity='4'" Get Name /Format:MOF 2^>NUL') Do #Echo %G
From a batch-file, for GUI double-click, it would look a little more like this:
#(For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
/NameSpace:\\Root\StandardCimv2 Path MSFT_NetConnectionProfile Where
"IPv4Connectivity='4'" Get Name /Format:MOF 2^>NUL') Do #Echo %%G) & Pause

ipconfig get first mac address

I am using this cmd command to get all the MAC addresses of the machine:
ipconfig /all
The problem is that it prints all the information and provides no switch to print just the mac. It also prints information for many network interfaces. I am interested just for the active one - this is presumably the first one. My next attemp was to parse just the physical address:
ipconfig /all | findstr "Physical Address"
but this also prints Link-local IPv6 Address and IPv4 Address.
What how can I get just the MAC address from ipconfig? I'd love to have some grep functionality. Is there a native regex implemented in cmd?
Either of these commands will filter the literal "Physical Address" string:
ipconfig /all | find "Physical Address"
ipconfig /all | findstr /c:"Physical Address"
Since this could list more than one MAC address and the first one isn't necessarily the "active" one consider using PowerShell:
PS C:\> Get-WmiObject Win32_NetworkAdapterConfiguration -filter "IPEnabled='true'" |select MACAddress
MACAddress
----------
xx:xx:xx:xx:xx:xx

How can I assign the name of the current wifi network to a variable in windows batch scripting?

I am trying to add a line to my start up scrip which would automatically start the VPN if I am connected to WiFi networks outside of my work.
I can get the names from netsh wlan commands, but I am not able to just "pick" the SSID/network name from that.
Is there a way to extract particular information from the netsh output or is there a way to output just the wifi name?
Thanks in advance.
You can use the findstr command to find a string in a commands, specifically the SSID.
Script:
#echo off
set "wifiNetwork=example network"
(netsh wlan show networks | findstr /C:": %wifiNetwork%" >nul 2>&1)&&goto :found
echo Wi-Fi network "%wifiNetwork%" not found
goto :end
:found
echo Wi-Fi network "%wifiNetwork%" found!
:end
pause
NOTE: The findstr command is case-sensitive unless you add the /I option

How to know the domain ping of a server

I need help. I have this command to execute.
wmic nicconfig get dnsdomain, ipaddress |findstr /i ipleiria.pt
I need to convert this command from a bat file to work next to another multicast program. I need to know the domain server ping of ipleiria.pt
wmic nicconfig where "dnsdomain='ipleiria.pt'" get dnsdomain, ipaddress
wmic /append:"textfile.txt" path win32_pingstatus where "address='127.0.0.1' and responsetime > 100" get responsetime,timestamprecord

Finding parent process ID on Windows

Problem
Given a process ID & command-line access on a remote Windows host, how can you find its parent's PID?
Solution
Given Marc B's answer, we can use WMIC (Command samples here) and do something like this:
wmic process where (processid=PROCID_HERE) get parentprocessid
C:\> wmic process get processid,parentprocessid,executablepath|find "process id goes here"
In powershell:
PS> wmic process where '(processid=4632)' get 'processid,parentprocessid,executablepath'
ExecutablePath ParentProcessId ProcessId
C:\Program Files\Docker\Docker\Resources\com.docker.db.exe 4488 4632
Based on joslinm's solution in the question, here's a snippet of how to use this in a batch script:
set PID=<this is the child process ID>
for /f "usebackq tokens=2 delims==" %%a in (`wmic process where ^(processid^=%PID%^) get parentprocessid /value`) do (
set PARENT_PID=%%a
)
Or you can do something like this in PowerShell:
Get-CimInstance -className win32_process | where-object {$_.ProcessId -eq processId_goes_here } | select ParentProcessId, Name
as well you can filter by name just substitute $_.ProcessId with $_.Name property

Resources