Search code examples
powershellpowershell-3.0powershell-4.0

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.


Solution

  • 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