Wednesday, January 22, 2014

Calculate csv column data and append it in same CSV.

Calculating your CSV column data (sum)  and appending it at the end.

I have named the csv file data.csv and kept it in c:\temp and the CSV file contents looks like this

After running below script.

 $csv= import-csv c:\temp\Data.csv  
 $Numcpu = $csv.Numcpu | measure-object -sum  
 $MemoryTotalGB = $csv.MemoryTotalGB | measure-object -sum  
 $object = New-Object PSObject  
 $object | Add-Member -Name Name -Value "Total" -MemberType NoteProperty  
 $object | Add-Member -Name NumCpu -Value $Numcpu.sum -MemberType NoteProperty  
 $object | Add-Member -Name MemoryTotalGB -Value $MemoryTotalGB.sum -MemberType NoteProperty  
 $object | Export-Csv c:\temp\Data.csv -Append  


Post a Comment