Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Powershell extract value from output

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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 by Get-NetAdapter

  • .Name returns that object’s Name property value.

Note:

  • If your system has no internet-connected adapter, the above command will fail, because applying an index such as [0] to "nothing" ($null or a command that produces no output) causes a Cannot index into a null array. error.

[1] In PowerShell 7.4+, calculated properties display italicized to indicate that fact.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading