How about this?
Get-WmiObject Win32_NetworkAdapter | select -expand macaddress
How about this?
Get-WmiObject Win32_NetworkAdapter | select -expand macaddress
Ah, but using Get-WmiObject Win32_NetworkAdapter (as js suggests after I discovered it for myself) works!
$mac = Get-WmiObject Win32_NetworkAdapterConfiguration | select -expand macaddress
$mac = $mac.replace(':','-')
$mac
Is this method portable across various models? I like this solution a lot. It’s simple.
I would think so, but you may get back more than one interface (bluetooth, wifi, …) and would have to filter it. Something like:
$connected = 2
Get-WmiObject win32_networkadapter | where { $_.netconnectionid -and
$_.netconnectionstatus -eq $connected }
btw, what does the “-expand” do (other than make it work for me)?
Thanks!
Without -expand you would get back a property that you would have to reference with $var.macaddress instead of just $var.
PS C:\users\j> get-netadapter | select macaddress MacAddress ---------- 8C-89-A5-B6-A6-99 PS C:\users\j> get-netadapter | select -expand macaddress 8C-89-A5-B6-A6-99
Yes, but .MacAddress is an array, and I can’t do .Replace() on the un-expanded version, so the -expand is not just stripping out the header, but is also converting it to a non-array format, seems to me.
I would go read up on it for myself, but I don’t know if I’d need to read about “select”, or “expand”, or “Get-NetAdapter”, or what. Googling for “powershell select -expand” is not being very productive.
Ah, doing more research, I find that the
@{Name="MoBettaMac";Expression={$_.MacAddress.Replace(":","-")}}
bit creates a “new” property that is not part of the normal results; it’s a “calculated property”, consisting of a Name key and an Expression key. So we’re creating a new property named “MoBettaMac”, that consists of the expression “[results].MacAddress.Replace(‘:’,‘-’)”.
Then to get the value I want, I’d use
$mac.MoBettaMac
I don’t need to use the .Replace method on it, because that’s already been done in the calculation, and the reason it worked in the calculation is because the .MacAddress property in this context is not an array, because it’s the .MacAddress property of the selected NIC, not the .MacAddress property of a standard output of the Get-NetAdapter (or cousin) cmdlet, which is an array because it deals with all the NICs in the box.
I think I’m beginning to get the hang of this a little better now.