所以我试图获取一堆文件的内容来替换标题字符串:
$replaces = dir | select Name, @{n='content';e={(get-content $_) -replace "header-foo","header-bar"}}
然后给了我一个列表:
Name Content
---- ----------
a.txt header-foo blah blah
b.txt header-foo blah blah blah
c.txt header-foo blah
然后我想将 this 的值传递给 set-content -value,如下所示:
$replaces | set-content -Path {"alt/" + $_.Name} -Value {$_.content}
现在只有我所有的文件都有内容$_.content
。我也尝试过-Value ($_.content)
,但这也没有做正确的事情。
只有当我使用 foreach 时它才会起作用:
$replaces | foreach { set-content -Path ("alt/" + $_.Name) -Value $_.content }
为什么会这样?为什么没有它就不能正常工作foreach
?