1

I have 2 CSV Files that contain different information. There is one column which is similar in both files. I want to merge the two files by checking the ID Column to get a new file with all information from both files. So it should be like this:

File A

Column 1 = ID
Column 2 = Text
Column 3 = other Text

File B

Column 1 = ID
Column 2 = some other text

I want to merge them now using powershell and "import-excel" module to get one new csv:

File C

Column 1 = ID
Column 2 = Text
Column 3 = other text
Column 4 = some other text
5
  • 1
    Hi, welcome, what have you tried and what was the problem? Commented Mar 12, 2020 at 16:34
  • Well actually i tried to google what or how i can merge them but i didn't find anything helpful for that. Whenever i try to google for "Import-Excel" i get a lot of links relative to excel but i have two simple textfiles that i want to merge. Commented Mar 12, 2020 at 17:58
  • Try Merge-Csv. Commented Mar 12, 2020 at 20:05
  • Or using Join-Object: Import-Csv .\FileA.csv | Join-Object (Import-Csv .\FileB.csv) -On Id | Export-Excel .\FileC.xlsx, see also: In Powershell, what's the best way to join two tables into one? Commented Mar 13, 2020 at 9:08
  • Thanks that worked very well and was easy to use. Commented Mar 19, 2020 at 17:59

1 Answer 1

0

There are a number of ways to relate 2 (or more) data sets, but probably the most common in PowerShell is to find a common & unique property and use hash tables to relate one to the other.

Assuming you have the ImportExcel module; Here's a simple example based on the somewhat limited information you gave:

I created:

FileA:
ID   Text1          Tex2
1    something1.1   something2.1
2    something2.1   something2.2

FileB:
ID   Text3
1    SoemthingElse
2    SomethingElse2
$FileA      = 'c:\temp\fileA.csv'
$FileB      = 'c:\temp\fileB.csv'
$MergeFile  = 'c:\temp\FileA_BMerged.xlsx'
$FileB_Hash = @{}

# Create a hash table from the data in FileB indexed on the ID column.
Import-Csv -Path $FileB |
ForEach-Object{ $FileB_Hash.Add( $_.ID, $_) }

# Now Import FileA via Import-Csv and correlate the data using the ID to tie the 
# 2 sets together.
#
# Notice the use of Select-Object with a calculated property.
Import-Csv -Path C:\temp\FileA.csv |
Select-Object *,@{Name = 'Text3'; Expression = { $FileB_Hash.($_.ID).Text3 } } |
Export-Excel -Path $MergeFile

Export-Excel is the last and least obscure part it's simply taking the objects created earlier in the pipeline steps and outputting them into an Excel sheet.

You do have to be sensitive to the field names. Make sure your keys are unique otherwise you'll have to handle differently. The basic principal is when you encounter the ID in the first collection you can use it to easily reference the object in the second set then use that data to extend the data in the first.

We can mix this in different ways including not use either set as a basis and simply creating a collection of PSCustomObjects. However the main principal should be the similar.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for your answer. I will have a try later with that. Just to be clear. The column ID in my files are matching in both files. So i want to check the ID in both files to get the Data together. I hope this will work so far. Thank you!
OK let me know. I'd also point out the other 2 solutions mentioned in the comments are very good.
I tried merge-csv in poowershell and this tool works very easy and powerfull in powershell. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.