Link to home
Start Free TrialLog in
Avatar of bndit
bndit

asked on

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

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.

http://powergui.org/thread.jspa?threadID=6773
# Author: Shay Levy
# http://powergui.org/thread.jspa?threadID=6773

$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

Open in new window

Avatar of bndit
bndit

ASKER

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

http://www.powergui.org/thread.jspa?threadID=10104

ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Open in new window


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
Avatar of bndit

ASKER

@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
In your line 8 you put import-csv twice.
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

Open in new window

Avatar of bndit

ASKER

@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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bndit

ASKER

@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.
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.
Avatar of bndit

ASKER

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.
Avatar of bndit

ASKER

Excellent assistance.