I have $body variable:

Name:john Doe

Location:New York

Role:engineer

I want, for each line, to get values after :.

Desired output:

John doe
New York
engineer

So far I have this code:

foreach ($part in $body.Split(':')) {
    echo $part
}

Current output:

Name
john Doe

Location
New York

Role
engineer

1 Answers

0
Mötz On Best Solutions

Without knowing to much about your data in $body, this should work:

$body = "Name:john Doe
Location:New York
Role:engineer"

$body.split([Environment]::NewLine) | ForEach-Object {
    $_.Split(":")[1]
}

First we need to split on every line that is in your variable. Then for each line, we need to split on ":" and because you only want the part from the right hand side, we use the index 1.