Home > Enterprise >  Get JUST the string value from a table row in powershell
Get JUST the string value from a table row in powershell

Time:06-09

i was wondering, as the title say. how to get just the string from a row in a powershell table.

so to simplify it, lets say i have a variable called $Fruits that returns this:

Fuits                   
-----                   
Banana
Apple
Watermelon
Peach
Pineapple

How would i go about printing JUST the word "Apple"? i know i can use $Fruits[1], however that returns another table.

Fuits                   
-----                   
Apple

How would i get JUST the word apple from here?

CodePudding user response:

You were 99.99% there.

You just need to select the specific column so that only its text is returned instead of an object.

So this $Fruits[1].Fuits

Other examples


# A single one
$Fruits[1].Fuits

#3 first records
$Fruits[0..2].Fuits
#OR
$Fruits | Select -First 3 -ExpandProperty Fuits

# All the records 
$Fruits | Select-Object -ExpandProperty Fuits
#OR
$Fruits.Fuits

Datasource used:

  $Fruits = @(
    [PSCustomObject]@{Fuits = 'Banana' }
    [PSCustomObject]@{Fuits = 'Apple' }
    [PSCustomObject]@{Fuits = 'Watermelon' }
    [PSCustomObject]@{Fuits = 'Peach' }
    [PSCustomObject]@{Fuits = 'Pineapple' }
  )
  • Related