user8746169
user8746169

Reputation:

Get-CimInstance win32_networkadapterconfiguration to return MAC and IP adress in powershell

I am trying to write a function that returns the MAC and IP addresses from the local network card with the use of "Get-CimInstance win32_networkadapterconfiguration" in a PowerShell function, but it's not returning anything.

function test{
          Get-CimInstance win32_networkadapterconfiguration
}

Upvotes: 3

Views: 5510

Answers (1)

Tony Hinkle
Tony Hinkle

Reputation: 4742

First you need to filter down the results to just return adapters that have an IP address assigned (where {$_.IPAddress -ne $null}). Then, optionally, use select to just get the two properties you want (select MACAddress, IPAddress).

function test{
    return Get-CimInstance win32_networkadapterconfiguration | where {$_.IPAddress -ne $null} | select MACAddress, IPAddress
}

This is going to return an object, not a string, so to access the properties individually you would do something like the following:

$Config = test
$Config.MACAddress
$Config.IPAddress

If you have more than one adapter with an IP address, you'll get an array of objects, and you'll need to loop through them, or use a different WHERE filter to further limit the results.

Upvotes: 2

Related Questions