#Select objects by adjacent values

1 messages · Page 1 of 1 (latest)

true crater
#

Apologies if I'm explaining this poorly -- I can't think of the right words to describe what I'm looking to accomplish.

I'm trying to extract values from a PowerShell table object that looks like this:

Id Value


1 apple
2 banana
3 carrot
4 cherry

Where the Value column value is "apple", I want to extract the "1" into a variable called "$apple".

I would much appreciate if someone could help me with the syntax for this.

#

The values of the "Id" column are not known beforehand, thus the motivation to store them into variables.

lofty cliff
#

What is a "powershell table object"? Are you talking about a DataTable? Can you post more code?

#

If you have $table , then you would do $apple = $table[0].id assuming your $table is always ordered.

native badger
#

Looks like you would interact with this object the same you would as a PsCustomObject array. So for your specific scenario assuming you have no duplicate data and your source data is in variable $data:

$apple = $data | where {$_.value -eq "apple"} | Select -expand Id