首页 > 解决方案 > Powershell如何同时将结果输出到多个文件中?

问题描述

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

就像是

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

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

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

标签: powershellpowershell-3.0powershell-4.0

解决方案


不,Out-File不能这样做


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

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

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

推荐阅读