Well, how about applying mjolinor code at the $ item level, for example:
foreach ($item in $array) { $item -replace '^|$','_' }
Although I expect the same result that you already got, newlines are embedded in your line.
I cannot configure the same condition in $ array myself, maybe you could post this code.
It works?:
foreach ($item in $array) { $item.Trim() -replace '^|$','_' }
source share