Cannot get to append two csv files in powershell

mast srl 96 Reputation points
2021-11-23T12:57:26.43+00:00

i have two CSV files: file1 and file2, i want to append file2 to the end of file1, both files have NO header information just raw data and the data fields are the same(same quantity of columns and and type of data).

i'm using:

$file1= "name of file1.csv" $file2 = "name of file2.txt" Get-Content $file2 | Export-Csv -Path $file1 -Append -NoTypeInformation -force

But the resulting output does not have file2 contents, what i see instead is 2 new empty lines at the end.

without the -force it whines about the mismatched properties.

i've also just tried piping a Select-Object after the get-content, to no avail, as well as: import-csv file1 | Export-Csv -Path file2 -Append -NoTypeInformation -force

¿what am i missing?

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. mast srl 96 Reputation points
    2021-11-23T16:04:13.393+00:00

    Ended up solving it by using: Get-Content file1 -raw | add-content file2
    not long after posting

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2021-11-23T16:00:16.717+00:00

    You're missing the fact that Export-CSV expects objects with named properties, not simple lines of text. You can

    If both files are identical in structure and you only want to concatenate the second file then try something like this:

    [Environment]::NewLine + (Get-Content C:\junk\file2.txt -raw) | Out-File c:\junk\file1.txt -Append
    

    Now you're just working with strings and files.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.