ShanayL
ShanayL

Reputation: 1247

Combine two CSV's - Add CSV as another Column

I am trying to join together two csv's. One of the CSVs contain the column I want to add to the other but when the csv is exported the column added is blank.

$csv1

VLAN
1
2
3

$csv2

Host
NETMAN
ADMIN
CLIENT

I get

VLAN, Host
1,
2,
3,

Desired output is

VLAN, Host
1,NETMAN
2,ADMIN
3,CLIENT

I tried

$csv1 = Import-Csv -Path ".\VLAN.csv"
$csv2 = Import-Csv -Path ".\Hostname.csv"

$csv1 | % {$i=0} { $csv1[$i] | Add-Member -NotePropertyName Hostname -NotePropertyValue $csv2[$i++].Hostname;}

$csv1 | Export-Csv combined.csv -NoType

Upvotes: 0

Views: 906

Answers (2)

iRon
iRon

Reputation: 23830

$csv1 | Join $csv2

Results

VLAN Host
---- ----
1    NETMAN
2    ADMIN
3    CLIENT

Upvotes: 0

Paul
Paul

Reputation: 5871

here you go:

$csv1 = Import-Csv -Path ".\VLAN.csv"
$csv2 = Import-Csv -Path ".\Hostname.csv"

$arr = @()
#If($csv1.count -eq $csv2.count){
    for($i=0; $i -lt $csv1.Count; $i++){
        $temp = New-Object psobject
        $temp | Add-Member -MemberType NoteProperty -Name VLAN -Value $csv1[$i].Vlan
        $temp | Add-Member -MemberType NoteProperty -Name HOST -Value $csv2[$i].Host
        $arr+=$temp
    }
#}

$arr | Export-Csv combined.csv -NoTypeInformation

Upvotes: 2

Related Questions