Merge two CSV using powershell

June 17, 2020 at 21:45:00
Specs: Windows 7
Hi all

I want to merge two csv and the output should be in table format using powershell script.

For Ex.

CSV1

Name class age
sanket be2 25
sourabh be1 23
akash be3 22


CSV2
year branch roll_no seat _no
4th IT 12 AC1234
2nd etc 14 EC4321
3rd mech 15 me1239

Expected outut:
Merge.csv

Name class age year branch roll_no seat _no
sanket be2 25 4th IT 12 AC1234
sourabh be1 23 2nd etc 14 EC4321
akash be3 22 3rd mech 15 me1239


I am using following script for merge two CSV

$file1 = Get-Content 'D:\Test1.csv'
$file2 = Get-Content 'D:\Test2.csv'

$content = for ($i = 0; $i -lt $file1.Length; $i++)
{
'{0},{1}' -f $file1[$i].Trim(), $file2[$i].Trim()
}
$content | out-file 'D:\Merge_output.csv'


After merging this two csv file the output came in single row .
I want it in seprate rows and columan


Any Help?


See More: Merge two CSV using powershell

Reply ↓  Report •

#1
June 18, 2020 at 00:24:03

Reply ↓  Report •

#2
June 18, 2020 at 03:33:16
I have tried both solutions but output come in single row as well when i use merge-csv cmd i revived an error
I have checked path Path and that are right

I have recived below error
Merge-Csv : The term 'Merge-Csv' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if
a path was included, verify that the path is correct and try again.
At D:\manjiri\scripts\Split_Scripts\new_merge.ps1:2 char:1
+ Merge-Csv -Path D:\manjiri\scripts\Split_Scripts\SCRIPT_TRIED\Job_Pro ...
+ ~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Merge-Csv:String) [], CommandNotFoundException

as wellaswhen i have tried another olution output came in single row like
Name class,age,year,branch,roll_no,seat _no
sanket,be2,25,4th, IT,1, AC1234

Need output in separate row and column(In Table Format)
Expected output

Name class age year branch roll_no seat _no
sanket be2 25 4th IT 12 AC1234

okay with batch script or powershell

Any help?


Reply ↓  Report •
Related Solutions


Ask Question