In powershell how do I get the value of first row under the the column "Name"
PS D:\Users\foo> Get-NetAdapter -InterfaceIndex $(Get-NetConnectionProfile | ? IPv4Connectivity -eq "Internet").InterfaceIndex
Name InterfaceDescription ifIndex Status MacAddress LinkSpeed
---- -------------------- ------- ------ ---------- ---------
Ethernet 8 Foobar #2 3 Up zz-zz-zz-zz 5 Gbps
The resulting value is "Ethernet 8"
>Solution :
PowerShell-native commands emit objects, (typically) not just text.
If the for-display output shows column names such as Name, the usual implication is that the output objects have such a property (predefined formatting data may present calculated property values with arbitrary names, however)[1]:
(
Get-NetAdapter -InterfaceIndex (
Get-NetConnectionProfile |
? IPv4Connectivity -eq Internet
).InterfaceIndex
)[0].Name
-
[0]selects the first output object emitted byGet-NetAdapter -
.Namereturns that object’sNameproperty value.
Note:
- If your system has no internet-connected adapter, the above command will fail, because applying an index such as
[0]to "nothing" ($nullor a command that produces no output) causes aCannot index into a null array.error.
[1] In PowerShell 7.4+, calculated properties display italicized to indicate that fact.