0

For example,

I have CSV file and imported to powershell

$fullname = Import-Csv “fullname.csv”
$fullname

Output is

FullName    
------------------
 John      Smith
 Kevin     Johnson

I have another CSV file and imported to

$Email = Import-Csv “Email.csv”
$Email

the output is

 Email
-------
[email protected]
[email protected]

I would like to concatenate this 2 variables and export to as one csv file, so I tried like this

 $fullname = Import-Csv “fullname.csv”
        $fullname
    $Email = Import-Csv “Email.csv”
        $Email

($fullname+$Email)|Export-Csv C:\fullnameandEmail.csv -NoTypeInformation

i also try like this

-join($fullname,$Email)|Export-Csv C:\fullnameandEmail.csv -NoTypeInformation

but it was not working,

I would like to make csv like below, how can I concatenate these 2 valuables?

FullName                 Email
---------                ----------
John    Smith            [email protected]
Kevin   Johnson          [email protected]

Thank you so much

3 Answers 3

2

It's helpful to understand that the $fullname and $email objects you have in memory after importing a CSV are actually arrays of objects. Each object has one or more properties that represent the column values from the CSV.

You can loop through the objects in either of the arrays and use the Add-Member cmdlet to add a new property to each object.

The following code loops through the $email array and for each item, it adds a property with the Email value to the corresponding item in the $fullname array. It then exports that merged array to a CSV file.

$fullname = Import-Csv "fullname.csv"
$email = Import-Csv "Email.csv"
$i = 0
$email | ForEach-Object { 
    Add-Member -inputobject $fullname[$i] -name Email -value $_.Email -membertype NoteProperty; 
    $i++}
$fullname | Export-Csv -notype -path "C:\fullnameandEmail.csv"
Sign up to request clarification or add additional context in comments.

Comments

2

So, for simplicity I would combine what the other two have suggested. Use a For loop, and then within the loop use Add-Member.

$fullname = Import-Csv “fullname.csv”
$Email = Import-Csv “Email.csv”
For($i=0;$i -lt $fullname.count;$i++){
    $FullName[$i] | Add-Member 'Email' $Email[$i].email
}
$FullName | Export-CSV -NoType Output.csv

Comments

1
$names = Import-Csv "fullname.csv"
$emails = Import-Csv "email.csv"

for ( $n = 0; $n -lt $names.Count; $n++ ) {
  New-Object PSObject -Property @{
    "FullName" = $names[$n].FullName
    "Email" = $emails[$n].Email
  } | Select-Object FullName,Email
}

2 Comments

No need for the Select statement in what you have there. No need to make whole new objects really.
Sure, there are multiple ways. Also, the Select-Object is in there because the hashtable is not ordered. It's not strictly necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.