Method 1: Using Get-NetAdapter
function Get-MacAddress-GetNetAdapter {
try {
$macAddresses = Get-NetAdapter | Where-Object { $_.Status -eq "Up" } | Select-Object -ExpandProperty MacAddress
if ($macAddresses) {
write-host "Get-NetAdapter - MAC Addresses: $macAddresses"
} else {
write-host "Get-NetAdapter - No active network adapters found."
}
} catch {
write-host "Error using Get-NetAdapter: $_"
}
}
Method 2: Using WMI
function Get-MacAddress-WMI {
try {
$macAddresses = Get-WmiObject Win32_NetworkAdapter | Where-Object { $_.NetConnectionStatus -eq 2 } | Select-Object -ExpandProperty MACAddress
if ($macAddresses) {
write-host "WMI - MAC Addresses: $macAddresses"
} else {
write-host "WMI - No active network adapters found."
}
} catch {
write-host "Error using WMI: $_"
}
}
Method 3: Using CIM
function Get-MacAddress-CIM {
try {
$macAddresses = Get-CimInstance -ClassName Win32_NetworkAdapter | Where-Object { $_.NetConnectionStatus -eq 2 } | Select-Object -ExpandProperty MACAddress
if ($macAddresses) {
write-host "CIM - MAC Addresses: $macAddresses"
} else {
write-host "CIM - No active network adapters found."
}
} catch {
write-host "Error using CIM: $_"
}
}
Method 4: Using ipconfig
function Get-MacAddress-Ipconfig {
try {
$ipconfigOutput = ipconfig /all
$macAddresses = ($ipconfigOutput -match '([0-9A-F]{2}-){5}[0-9A-F]{2}' -split "`n").Trim()
if ($macAddresses) {
write-host "Ipconfig - MAC Addresses: $macAddresses"
} else {
write-host "Ipconfig - No MAC addresses found."
}
} catch {
write-host "Error using Ipconfig: $_"
}
}
0 Comments