iron
iron

Reputation: 1

Powershell Export-Csv -Append in new column

Just want to ask how to Export the data in existing CSV file in new column. I have this following code.

$Ex=Compare-Object $ImportWin7 $Importafipd1 -includeequal 

$Ex | Select-Object SideIndicator | Export-Csv -Append -Force  -NoTypeInformation "C:\NotBackedUp\EndpointAudit\Win7machinetest2.csv"

but it appears that the Data from $EX was appended not on the first row of the column.

Upvotes: 0

Views: 5225

Answers (1)

Kirill Pashkov
Kirill Pashkov

Reputation: 3226

You can't just add an additional column to existing csv document. You would want to recreate it again with number of columns you need. Like so:

$ExistingCSV = Import-Csv "C:\NotBackedUp\EndpointAudit\Win7machinetest2.csv"

$Ex=Compare-Object $ImportWin7 $Importafipd1 -includeequal | Select-Object SideIndicator

$obj = @()
$i = 0

foreach ($row in $ExistingCSV)
{
    $item = New-Object PSObject -ArgumentList $row
    $item | Add-Member -MemberType NoteProperty extra_column -Value $Ex[$i]
    $obj += $item
    $i++
}
$obj | Export-Csv -Append -Force  -NoTypeInformation "C:\NotBackedUp\EndpointAudit\Win7machinetest2.csv"

Upvotes: 3

Related Questions