0

I have a variable $c.

$c returns

apples
oranges
bananas

How can I make these separate objects?

$fruit = $C |  foreach { $_ -like 'oranges'}

if($fruit)
{
  write-host "Success" -ForegroundColor Green
}
else
{
  write-host "Failed" -ForegroundColor red
}

This doesnt work unless I use wildcards?

Im trying to get an exact match checking each line.

Any help is appreciated. I'm stuck on this one.

3
  • 1
    What is C? A tring, array, hashtable? Commented Nov 27, 2020 at 20:53
  • $c = (invoke-webrequest -Uri "drive.google.com/… I'm fetching contents of a text document from my googledrive Commented Nov 27, 2020 at 23:06
  • Please add the full command you use for getting C$ in the question. Chances are you need to get $C.Content | ConvertFrom-Json to get an object that has your apples, bananas and other fruits. Commented Nov 28, 2020 at 10:27

1 Answer 1

3

Use the -split operator:

$multiLineString = @'
apples
oranges
bananas
'@

$arrayOfStrings = $multiLineString -split '\r?\n' 

\r?\n will either match CR LF, otherwise LF on its own, so it'll work for both UNIX- and Windows-style line separators in the input string.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm fetching contents of a text document from my googledrive which has a simple list of fruits. $c = (invoke-webrequest -Uri "drive.google.com/… When I check $c.length - Each letter seems to be an object?

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.