I need to work with csv files in PowerShell that have a duplicate column header. The reasons which they have a duplicate column are beyond me. Such is life.
I want to use Import-Csv so that I can easily deal with the data, but since the duplicate column exists I get this error:
Import-Csv : The member "PROC STAT" is already present.
At C:\Users\MyName\Documents\SomeFolder\testScript1.ps1:10 char:9
+ $csv2 = Import-Csv $files[0].FullName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Import-Csv], ExtendedTypeSystemException
+ FullyQualifiedErrorId : AlreadyPresentPSMemberInfoInternalCollectionAdd,Microsoft.PowerShell.Commands.ImportCsvCommand
I could manually fix the problem by going into every csv file and deleting the duplicate column. But this is not an option. There are hundreds of them, and the script needs to be run periodically. Ideally I am looking for a way to programatically remove that column (Import-Csv won't work) or programatically change the name of the column (so that I can then Import-Csv and delete it). Any suggestions?
My code to loop through all the files:
$files = Get-ChildItem "C:\Users\MyName\Documents\SomeFolder\Data" -Filter *.csv
foreach($file in $files) {
$csv = Import-Csv $file.FullName
}
Get-Content $file.FullName -First 1
to get the first line (headers) from the file. But I am not sure how to modify/write out to a new file. – Thomas