Use powershell ForEach-Object to match and replace string with regex
One thing you're missing is that the -replace operator works just fine on an array, which means you don't need that foreach-object loop at all:
(Get-Content "test1.xml") -replace '^name-.*$', 'name-6a5e4r3h' | Set-Content test2.xml
You're not changing the $_ variable.
You might try:
$lines = Get-Content $file$len = $lines.countfor($i=0;$i-lt$len;$i++){ $lines[$i] = $lines[$i] -replace $bad, $good}$lines > $outfile