user3331975
user3331975

Reputation: 2805

Split values returned

I have a script which returns few names as below. The value "mark" "ted" are combined since the function within the script is returning this together but in two different lines.. Hence $name always get these two names together (which runs in a loop) and I wanted to split them and call these two values separate within the script. Is this possible? Could you please help?

$list | % {
    $name = student.identifier
    Write-Output $name
    Write-Output Hello

Current Output:

jake
Hello
mark
ted
Hello

Upvotes: 2

Views: 63

Answers (1)

FoxDeploy
FoxDeploy

Reputation: 13537

Sounds like you want to split on each line break, am I right?

For Example:

$list = "jake
mark
ted"

$list.Count
>1

That means there's only one element in our list, which happens to be three lines long. Let's split it.

$list.Split("`n")

We're calling the Split method on our variable, and telling it to split on the special ``n` character, which is a PowerShell line-break.

If I were to take the count now, check it out.

$list = $list.Split("`n")
$list.Count
>3

And now to run your code (modified a bit)

 $list | % {
    $name = $_
    Write-output $name
    Write-Output "Hello"
    }
jake
Hello
mark
Hello
ted
Hello

Sounds like what you're asking for. If I misunderstood, please let me know.

Upvotes: 2

Related Questions