1

I am having set of CSV files in a folder. I want to convert the files into TSV file format. I want to use Powershell for the same.

I tried using the below script. it is throwing error.

Get-ChildItem "C:\Users\myname\Desktop\sampledata" | ForEach-Object { Import-Csv $_.FullName | Export-Csv {$_.Name} -Delimiter "`t" -Encoding UTF8 }

I am getting errors similar to below:

Import-Csv : The member "1" is already present. At line:8 char:72 + ... Desktop\sampledata" | ForEach-Object { Import-Csv $_.FullName | Expor ... + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Import-Csv], ExtendedTypeSystemException + FullyQualifiedErrorId : AlreadyPresentPSMemberInfoInternalCollectionAdd,Microsoft.PowerShell.Commands.ImportCs

How can I resolve this issue ?

4
  • 1
    {$_.Name} is a script block. Remove { and } or use (Join-Path 'c:\somewhere' $_.Name) Commented Oct 8, 2016 at 0:07
  • yes. I removed it. Still facing the same issue. I think it is something related to Import-csv Commented Oct 8, 2016 at 1:03
  • 1
    Apparently your CSV file has a duplicate field name. Commented Oct 8, 2016 at 1:14
  • Yes. You are right. I chose the corresponding answer also. Commented Oct 10, 2016 at 20:54

3 Answers 3

3

The error indecated same value exist in the cells in first line of that csv file . It is a normal behavior of command import-csv .

You can find that CSV out .

As a workaround , you may specify the table header with parameter "-Header":

Import-Csv xxx.csv -Header a,b,c,d
Sign up to request clarification or add additional context in comments.

Comments

1

As of this writing, the question contains an additional, incidental mistake: {$_.Name} is used to specify the output filename: however, {$_.Name} is a script block that in a string context evaluates to its literal contents, i.e., literal $_.Name. By contrast, the intent is to specify the Name property of the current pipeline object ($_) as the filename: $_.Name by itself will do - no quoting or other qualification is needed (although "$($_.Name)" too would work, inside a double-quoted string).

To complement Elton Ji - MSFT's helpful answer:

Note: The following applies analogously to Import-Csv's in-memory counterpart, ConvertFrom-Csv.

The error message you're getting (member "<name>" is already present) tells you that duplicate values exist in the input CSV file's 1st line (as woxxom also notes in a comment on the question).

Import-Csv expects an input file to start with a header row, i.e it expects the first line (row) to contain column names, with the subsequent rows containing the data.

  • These column names become the properties (members) of the objects ([pscustomobject] instances) that PowerShell constructs from the data rows, and given that an object (class) cannot have multiple properties with the same name, duplicate column names aren't allowed.

It sounds like your input file is missing such a header row, which caused the first data row to be interpreted as the header row, and because it happened to contain duplicate values, the command broke.

If a header row is missing, you can make up for that with the -Header parameter, which allows you to specify column names ad-hoc, with Import-Csv -Header <colName1>, <colName2>, ....

  • If you specify fewer column names than data columns, the extra columns are ignored.

  • If you specify more column names than data columns, additional properties are created, but they'll have value $null.

  • As of PowerShell 7.3.3, there is no way to let PowerShell create headers for you automatically. See the feature request in GitHub issue #19334.

Comments

-1

try this

Get-ChildItem c:\temp\ -Filter "*.csv" | foreach{(Import-Csv $_.FullName -Delimiter ";") | export-csv $_.FullName -NoTypeInformation -Delimiter "`t" -Encoding UTF8}

1 Comment

Except for changing the input delimiter to ; and adding -NoTypeInformation - neither of which the OP asked for - your solution is essentially the same as what the OP himself tried. Note that even if the input delimiter had been a problem, it wouldn't have caused the symptom described by the OP.

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.