I try this command
get-wmiobject win32_networkadapter -filter \"netconnectionstatus = 2\" |
Select macaddress | Select -Last 1
and I want to
Select-Object selects the properties of objects (among other things). The result remains a list of objects, though (the tabular output displays the properties of the objects). If you want just the value of a particular property, you need to expand it:
get-wmiobject win32_networkadapter -filter "netconnectionstatus = 2" |
Select -Expand macaddress | Select -Last 1
Edit: As @FrodeF mentioned in the comments, you can also merge the 2 Select-Object
statements into one:
get-wmiobject win32_networkadapter -filter "netconnectionstatus = 2" |
Select -Expand macaddress -Last 1
Note, however, that in this case the -Last 1
applies to the input of that Select-Object
, not the selected property. This works for MAC addresses, because each network adapter can have only one, but it may not produce the desired result in other scenarios. Example:
PS C:\> Get-WmiObject Win32_NetworkAdapterConfiguration |
>> ? { $_.IPEnabled -eq $true } |
>> select -Expand IPAddress -Last 1
>>
192.168.56.99
fe80::21ba:e68c:ebd0:1046
PS C:\> Get-WmiObject Win32_NetworkAdapterConfiguration |
>> ? { $_.IPEnabled -eq $true } |
>> select -Expand IPAddress | select -Last 1
>>
fe80::21ba:e68c:ebd0:1046
Here the IPAddress
property has 2 values (IPv4 address and IPv6 address). So, in the first case select -Expand IPAddress -Last 1
selects the last adapter configuration object, then expands the IP address list. In the second case select -Expand IPAddress | select -Last 1
expands the IP address lists of all adapters, then selects the last item of the resulting list.