powershellmergefile-iointerleave

How create combined file from two text files in powershell?


How can I create file with combined lines from two different text files and create new file like this one:

First line from text file A
First line from text file B
Second line from text file A
Second line from text file B
...

Solution

  • $file1content = Get-Content -Path "IN_1.txt"
    $file2content = Get-Content -Path "IN_2.txt"
    
    $filesLenght =@($file1content.Length, $file2content.Length)
    
    for ($i = 1; $i -le ($filesLenght | Measure-Object -Maximum).Maximum; $i++)
    {   Add-Content -Path "OUT.txt" $file1content[$i]
        Add-Content -Path "OUT.txt" $file2content[$i]
    }