Exporting in CSV is giving incorrect result

In the below code I am trying to merge two csv files on the basis of one common parameter - Employee id. I am getting the desired result in powershell , however, while exporting to a csv I am getting incorrect data.

$UserList=Import-Csv -Path "j:\testad.csv"
$UserData = Import-Csv "j:\testswn.csv"
Foreach ($User in $UserList)
    Foreach($Data in $UserData)
        If($User.EmployeeId -eq $Data.EmployeeId)

          $e=$a+" "+ $b+ " " + $c+" "+$d+" "+$e+" "+$f+" "+$g+" "+$h+" "+$i+" "+$j+" "+$k
            $temp= $e
$temp|Export-Csv "j:\testresult2.csv" -NoTypeInformation
Aakash AggarwalAsked:
Who is Participating?
oBdAConnect With a Mentor Commented:
You're looking at the wrong file, This is not the output generated by my script, this is leftover from your old script.
Since you didn't provide any sample csv, I've created them based on the properties specified:
EmployeeID, Givenname, sn, EmailAddress, StreetAddress, City, State, Country, PostalCode
ei01, gn01, sn01, em01, sa01, ci01, st01, co01, pc01
ei02, gn02, sn02, em02, sa02, ci02, st02, co02, pc02

Open in new window

EmployeeID, Telephone, PersonalEmail
ei01, te01, pe01
ei02, te02, pe02

Open in new window


Open in new window

Tr this:
$UserList = Import-Csv -Path "j:\testad.csv"
$UserData = Import-Csv -Path "j:\testswn.csv"
$Lookup = @{}
$UserData | ForEach-Object {$Lookup[$_.EmployeeID] = $_}
$UserList | Select-Object -Property `
	EmployeeID, Givenname, sn, EmailAddress, StreetAddress, City, State, Country, PostalCode, `
	@{n='Telephone'; e={$Lookup[$_.EmployeeID].Telephone}}, @{n='PersonalEmail'; e={$Lookup[$_.EmployeeID].PersonalEmail}} |
	Export-Csv -NoTypeInformation -Path "j:\testresult2.csv"

Open in new window

Aakash AggarwalAuthor Commented:
Now I am getting the data in CSV, however the result is giving data for all emp id in csv file2, however, it should give only for those which have same emp ids in table 1.
Aakash AggarwalAuthor Commented:
Everything is fine but the thing is in one CSV file for some Employees id they are in 6 digits and others in 5 digits, so there the matching result we are getting is incorrect.

Ex- If in my first CSV there are 2 records of Employee id - 123456, 034567 where as in second CSV file it is like- 123456, 34567(0 is not there), so how can we change the equate equation for employee ids so that we can match 6 digits employee id with 5 digits in second csv?

$UserData | ForEach-Object {$Lookup[$_.EmployeeID] = $_}
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.