Getting NIC IP addresses and MAC addresses

by Oct 4, 2011

WMI can return network information such as your current IP address and MAC address. Here is a sample how PowerShell can utilize and beautify the information. The result is a table with all network adapters that have a MAC address, neatly displaying IPv4 and IPv6 in separate columns.

function Get-IPandMAC {
  param(
    $ComputerName
  )

  $nicname = @{
    Name = 'NICname'
    Expression = { ($_.Caption -split '] ')[-1] }
  }

  $ipV4 = @{
    Name = 'IPv4'
    Expression = { ($_.IPAddress -like '*.*.*.*') -join ',' }
  }

  $ipV6 = @{
    Name = 'IPv6'
    Expression = { ($_.IPAddress -like '*::*') -join ',' }
  }

  Get-WmiObject -Class Win32_NetworkAdapterConfiguration @PSBoundParameters | 
  Select-Object -Property $nicname, $ipv4, $ipv6, MacAddress | 
  Where-Object { $_.MacAddress -ne $null } 
}

Twitter This Tip!
ReTweet this Tip!