powershellpowershell-2.0powershell-3.0powershell-v6.0

Powershell merge 2 columns from different files without a key field


I have 2 csv files and have file1.column1, file1.column2, file2.column1, file2.column2. I would like to have out put like below

File1:

Column1, Column2
1,a
2,b
3,c

File2:

Column1, Column2
x, abc
y, def
z, ghi

Output I am expecting is: File3:

File1.column1, File2.column2
1, abc
2, def
3, ghi

Solution

  • @PetSerAl is correct, it appears that you want to merge these on the line number, i.e the index of each row. Here's a contrived example with custom objects, just replace $File1 and $File2 with the output from Import-Csv:

    $File1 = @(
    [PSCustomObject]@{
        Column1 = 1
        Column2 = "a"
    }
    [PSCustomObject]@{
        Column1 = 2
        Column2 = "b"
    }
    [PSCustomObject]@{
        Column1 = 3
        Column2 = "c"
    }
    )
    
    $File2 = @(
        [PSCustomObject]@{
            Column1 = "x"
            Column2 = "abc"
        }
        [PSCustomObject]@{
            Column1 = "y"
            Column2 = "def"
        }
        [PSCustomObject]@{
            Column1 = "z"
            Column2 = "ghi"
        }
    )
    
    for ($i = 0; $i -lt $File1.Count; $i++ )
    {
        [PSCustomObject]@{
            "File1.Column1" = $File1[$i].Column1
            "File2.Column2" = $File2[$i].Column2
        }
    }