0

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?

0

1 Answer 1

1

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' }
  )
Sign up to request clarification or add additional context in comments.

4 Comments

Awesome! thanku. i feel kinda stupid now ngl.
If i then want to foreach loop the original table. and write-host "this fruit is called: $Fruits[$i].Fruits would i then use foreach($Fruits.Fuits in $Fruits) ?
@Elly Foreach ($F in $Fruits.Fuits) and then write-host "this fruit is called: $F" or Foreach ($F in $Fruits) and then Write-Host "this fruit is called: $($F.fuits)", the latter being useful if you had multiple properties you might want to access.
again thanku! this helped me a lot! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.