Root Loop
Root Loop

Reputation: 3162

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

Is there a way that I can out-file to multiple files in one shot?

something like

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

I want to keep few copies of the same results.

I tested few ways, nothing worked.

Upvotes: 1

Views: 6412

Answers (2)

TessellatingHeckler
TessellatingHeckler

Reputation: 29033

Does it have to be one-shot? Tee-Object is built to pass the pipeline straight through it and out the other side, while saving it to a file as well. It has the default alias tee so you could:

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

Upvotes: 2

Matt
Matt

Reputation: 46730

No, Out-File cannot do this


However, Add-Content and Set-Content do support this for their -Path parameters

An example, from the links above, for Set-Content has a similar example for this as well using wildcards.

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

But, -Path supports arrays so you just need to change the cmdlet you are using and should be off to the races.

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

Upvotes: 3

Related Questions