Solved

Use PowerShell to merge info from two different CSV files into a new CSV file

Posted on 2010-08-27
4,494 Views
I have a CSV file that contains this information (approx 10k entries)
EMPLOYEE_NUMBER, FIRST_NAME,MIDDLE_NAME,LAST_NAME,TITLE (some other columns..)
12345,John,K.,Doe,Manager
23425,Jane,,Doe,Director
23434,James,L.,Doe,Clerk
44444,David,,Smith,Sales Rep
,Robert,Jones,Sales Rep

I have a second CSV file that contains this information (approx 50 entries)
Name,NTAccountName,employeeNumber
John K. Doe,domain\John.Doe,12345
Jane Doe, domain\Jane.Doe,23425,

Goal: to merge the employee's title information from the first CSV file with the second file. So the output should be this:

Name,NTAccountName,employeeNumber,Title,
John K. Doe,domain\John.Doe,12345,Manager
Jane Doe, domain\Jane.Doe,23425,Director

I found on the Internet, but I'm having a hard time tweaking it to do what I want. Here's the post that I found. Since I have a common field in both CSV, I'd think I could use the employeeNumber to do the match.

# Author: Shay Levy

$csv1 = import-csv D:\Scripts\temp\logonInfo1.csv$csv2 = import-csv D:\Scripts\temp\logonInfo2.csv

$result1 =$(compare-object $csv1$csv2 -IncludeEqual | where {$_.SideIndicator -eq "=="} | foreach {$_.InputObject})
$result2 =$(compare-object $csv2$csv1 -IncludeEqual | where {$_.SideIndicator -eq "=="} | foreach {$_.InputObject})

$members1 =$csv1 | gm -mem  NoteProperty |  foreach {$_.name}$members2 = $csv2 | gm -mem NoteProperty | foreach {$_.name}

$newMembers =$(compare-object $members1$members2 | where {$_.SideIndicator -eq "=>"} | foreach {$_.InputObject})

for($i=0;$i -lt $newMembers.length;$i++){
$prop =$newMembers[$i]$result1 | add-member noteproperty $prop$null

for($x=0;$x -lt $result1.length;$x++){
$newMembers | foreach {$result1[$x].$prop = $result2[$x].$prop } } }$result1 | export-csv D:\Scripts\temp\new.csv -NoTypeInformation
get-content D:\Scripts\temp\new.csv

0
Question by:bndit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 5
• 2

LVL 2

Author Comment

ID: 33547607
Here's another possible solution (again from Shay), but it's not working for me

0

LVL 71

Accepted Solution

Chris Dent earned 125 total points
ID: 33548599
This should work.

Chris
$CSV1 = Import-Csv "First.csv"$CSV2 = Import-Csv "Second.csv"

# Since the second is smaller we will loop through the first once and use Where-Object
# to find entries in the second

$CSV1 | ForEach-Object { # Store the EmployeeNumber from the first CSV$EmployeeNumber = $_.EMPLOYEE_NUMBER # And pull out the title field$Title = $_.TITLE # Find the corresponding entry in CSV 2 and prepare some output # Making a new field for Title and filling it with the title from CSV1.$CSV2 |
Where-Object { $_.employeeNumber -eq$EmployeeNumber } |
Select-Object Name, NTAccountName, EmployeeNumber,
@{n='Title';e={ $Title }} # Export everything to a new CSV file } | Export-Csv "NewFile.csv"  0 LVL 13 Expert Comment ID: 33549086 In my solution I make use of the hashtable data structure to find pairs. (Don't give me points is Chris' solution works for you, this is just a brain excercise for me). (It only works if the second csv only include every employeenumber once.) $secondinfo = Import-Csv c:\ee\second.csv | Group-Object -Property employeenumber -AsHashTable
Import-Csv c:\ee\first.csv | %{
$title =$_.title
$secondinfo.($_.employee_number) | Select-Object name, ntaccountname, employeenumber,
@{n="Title";e={$Title}} } | Export-Csv c:\ee\new.csv -NoTypeInformation  0 LVL 71 Expert Comment ID: 33549918 I think the HashTable is well worth introducing, I'd have gone down the same path if both files were large. My version doesn't scale anywhere near as well as a comparison against a Hashtable will. Chris 0 LVL 2 Author Comment ID: 33551272 @Chris Your script works great. However, in the second CSV file there are some entries that do not have an employee number value. I noticed that the script ignores these accounts and are not included in the output file. Is there any way to include them in the merged output file? I'm thinking it'd be some type of if-statement..."If the employee number is empty don't bother looking in the large file, just pass the record to the output file". That'd solve my problem! @soostibi I tried your script, but I came across some problems. I'm attaching my script as I ran it and the error that I got. HT-script.png HT-error.png 0 LVL 13 Expert Comment ID: 33552518 In your line 8 you put import-csv twice. 0 LVL 13 Expert Comment ID: 33552709 If we can not use the employeenumber as a join between the two CSVs, but we can use the full name, then the script can be: $secondinfo = Import-Csv c:\ee\second.csv | Group-Object -Property name -AsHashTable
Import-Csv c:\ee\first.csv | %{
$name =$_.first_name + " " + @(if($_.middle_name){$_.middle_name+" "}) + $_.last_name$Title = $_.title$emplnum = $_.employee_number$secondinfo.($name) | Select-Object name, ntaccountname, @{n="employeenumber"; e={$emplnum}},
@{n="Title";e={$Title}} } | Export-Csv c:\ee\new.csv -NoTypeInformation  0 LVL 2 Author Comment ID: 33554447 @soostibi Good catch; the issue was the double Import-Csv. The script is now working but I get the same result as with Chris' script; the records without employee number in the second CSV file are ignored and don't show up in the merged file. At this point, the script helps me with the records with employee numbers, but I'd have to manually merge the records without employee number in the second file. Let me tell you more about these files: File1: This file is an extract directly from an HR database. Thus, all records will match an individual, and this individual will have an employee number. File2: This file is an extract directly from AD. Thus, not all records will match an individual. In other words, I have generic accounts such as service accounts, etc., which do not have an employee number. This is why the full name is not a good join value for both files. Again, if there was a way for the script to instead of ignoring the records without employee numbers in the second file to just pass them to the merged file, that'd be great. 0 LVL 13 Assisted Solution soostibi earned 125 total points ID: 33554543 What about this then? $firstinfo = Import-Csv c:\ee\first.csv | Group-Object -Property employee_number -AsHashTable
Import-Csv c:\ee\second.csv |
Select-Object name, ntaccountname, employeenumber,
@{n="title"; e={$firstinfo.($_.employeenumber).item(0).title}} |
Export-Csv c:\ee\new.csv -NoTypeInformation

0

LVL 2

Author Comment

ID: 33554580
@soostibi
That works! Thanks. Now, could comment the script so that I know the logic a bit better. I kinda get parts of it but I'd like to fully understand it.  Thanks again.
0

LVL 13

Expert Comment

ID: 33554653
1: I import the first csv and convert it to a hashtable at once by the employeenumber attribute. Actually this is a Group-object cmdlet, but as the employeenumber is unique, it actually makes a hashtable that has an element of the csv in each 'row' referenced by the employeenumber. I use this as there is no faster way to do it.
2: I import the second csv and pipe it into a
3: select-object cmdlet, to select the relevant properties and to create a new one, that is
4: the 'title' property, that come from addressing the item from the hashtable by the employeenumber of the actual element of the second csv that comes through the pipeline. As I mentioned, the hashtable is not a simple hashtable because of the group-object, so I have to grab the real information that is wrapped into a parametrized property named 'item'.
5: what borns after this is exported to a new csv.
0

LVL 2

Author Comment

ID: 33554660
Awesome, thanks both of you for your help. I think the fair thing to do is for me to split the points as you both helped me solve this problem.
0

LVL 2

Author Closing Comment

ID: 33554666
Excellent assistance.
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month3 days, 1 hour left to enroll