So I'm trying to get the contents of a bunch of files to replace the header string:
$replaces = dir | select Name, @{n='content';e={(get-content $_) -replace "header-foo","header-bar"}}
That then gives me a list of:
Name Content
---- ----------
a.txt header-foo blah blah
b.txt header-foo blah blah blah
c.txt header-foo blah
Then I want to pass the value of this to set-content -value like so:
$replaces | set-content -Path {"alt/" + $_.Name} -Value {$_.content}
Only all of my files have the content $_.content
now. I also tried -Value ($_.content)
but that doesn't do the right thing either.
It's only when I use a foreach does it work:
$replaces | foreach { set-content -Path ("alt/" + $_.Name) -Value $_.content }
Why is this the case? Why does it not work correctly without the foreach
?