Converting Custom Object Arrays To String Arrays In Powershell
Answer :
This will give you what you want:
$strArray = $ms | Foreach {"$($_.Option)$($_.Title)"}
Select-Object is kind of like an SQL SELECT. It projects the selected properties onto a new object (pscustomobject in v1/v2 and Selected.<orignalTypeName> in V3). Your second approach doesn't work, because $_.Option
in a string will only "interpolate" the variable $_
. It won't evaluate the expression $_.Option
.
You can get double-quoted strings to evaluate expressions by using subexpressions, for example, "($_.Option)".
Comments
Post a Comment