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

问题描述 投票:1回答:2

有没有办法我可以一次性out-file多个文件?

就像是

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

我想保留一些相同结果的副本。

我测试了几种方法,没有任何效果。

powershell powershell-v3.0 powershell-v4.0
2个回答
3
投票

不,Out-File不能这样做


然而,Add-ContentSet-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

1
投票

它必须是一次性的吗? Tee-Object用于将管道直接穿过它并从另一侧传出,同时将其保存到文件中。它有默认别名tee,所以你可以:

{script block} |tee $file1 |tee $file2 |tee $file3
© www.soinside.com 2019 - 2024. All rights reserved.