Powershell如何同时将结果输出到多个文件中?

Powershell how can I output results into multiple files at the same time?

有没有一种方法可以 out-file 一次性处理多个文件?

类似于

{script block} | out-file $file1, $file2, $file3

我想保留几份相同的结果。

我测试了几种方法,都没有用。

不,Out-File 不能这样做


但是,Add-Content and Set-Content 确实支持其 -Path 参数

来自上面链接的示例,Set-Content 有一个类似的示例,也使用通配符。

PS> Get-ChildItem -Path .\Test*.txt

Test1.txt
Test2.txt
Test3.txt

PS> Set-Content -Path .\Test*.txt -Value 'Hello, World'

PS> Get-Content -Path .\Test*.txt

Hello, World
Hello, World
Hello, World

但是,-Path 支持数组,因此您只需更改正在使用的 cmdlet,就可以开始比赛了。

{script block} | Set-Content -Path $file1, $file2, $file3

一定要一次性的吗? Tee-Object 的构建是为了让管道直接通过它并从另一侧流出,同时也将其保存到文件中。它具有默认别名 tee,因此您可以:

{script block} |tee $file1 |tee $file2 |tee $file3