Display host name and raw MAC Address

This topic contains 2 replies, has 2 voices, and was last updated by  Tony Wainwright 2 years ago.

  • Author
    Posts
  • #30772

    Tony Wainwright
    Participant

    Hi
    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 "-",""}
    $Adapters`
    but this only displays the raw MAC address.
    Why is the Name stripped out when the ForEach cmdlet is run?

    Thanks
    Tony

  • #30775

    Richard Siddaway
    Moderator

    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

    Tony Wainwright
    Participant

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

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

    Cheers
    Tony

You must be logged in to reply to this topic.