Skip to content

Instantly share code, notes, and snippets.

@Axlfc
Created October 23, 2024 07:03
Show Gist options
  • Save Axlfc/d0359fc3dac88edb541af4c04752cfe0 to your computer and use it in GitHub Desktop.
Save Axlfc/d0359fc3dac88edb541af4c04752cfe0 to your computer and use it in GitHub Desktop.
function Get-EthernetMacAddress {
try {
# Get network adapters
$adapters = Get-NetAdapter | Where-Object { $_.InterfaceDescription -like '*Ethernet*' }
if ($adapters.Count -eq 0) {
Write-Error "No Ethernet adapter found."
return $null
}
# Get MAC address of the first Ethernet adapter found
$ethernetAdapter = $adapters[0]
$macAddress = $ethernetAdapter.MacAddress
return $macAddress
} catch {
Write-Error "Failed to retrieve Ethernet adapter MAC address."
}
}
$ethernetMacAddress = Get-EthernetMacAddress
if ($ethernetMacAddress) {
Write-Host "Ethernet MAC Address: $ethernetMacAddress"
# Optionally, you can use the retrieved MAC address here for further processing
} else {
Write-Host "Ethernet MAC address could not be retrieved."
}
$Age = Read-Host "Ethernet MAC Address"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment