Display host name and raw MAC Address

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Tony Wainwright Tony Wainwright 1 year, 9 months ago.

  • Author
  • #30772
    Profile photo of Tony Wainwright
    Tony Wainwright

    I am trying to enumerate all my NIC's and display the name and raw MAC address to look something like:
    Name MacAddress
    —- ———-
    NIC4 C8-1F-66-F7-1B-78
    NIC3 C8-1F-66-F7-1B-77
    NIC2 C8-1F-66-F7-1B-76
    NIC1 C8-1F-66-F7-1B-75`

    I am using the following
    `# Get Adapter Name & MAC Address for each adapter and remove Dashes From MAC Address
    $Adapters = Get-NetAdapter | Select Name, MACAddress | ForEach {$_.MacAddress -replace "-",""}
    but this only displays the raw MAC address.
    Why is the Name stripped out when the ForEach cmdlet is run?


  • #30775
    Profile photo of Richard Siddaway
    Richard Siddaway

    Your code will only emit the modified macaddress becasue you don't include it in the foreach loop and output it.

    You should do this

    Get-NetAdapter | select Name, @{N='MacAddress'; E={$_.MacAddress -replace '-',"}}

    Create a calculated field with select-object to perform the modification

  • #30776
    Profile photo of Tony Wainwright
    Tony Wainwright

    Thanks Richard, worked great...you just missed the closing " at the end of the replace

    Get-NetAdapter | select Name, @{N='MacAddress'; E={$_.MacAddress -replace '-',""}}


You must be logged in to reply to this topic.