Import-csv same member

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Import-csv same member

Post by sekou2331 »

Hi,

I have a CSV file that has a couple columns that are the same. It looks like Import-csv doesn't like this and gives me the error below. Is there any I can ignore or bypass the repeat column so I can avoid this error?

ERROR: Import-Csv : The member "\\computername\Process(APP)\Virtual Bytes" is already present.
DeskUsage.ps1 (14, 6): ERROR: At Line: 14 char: 6
ERROR: + $a = Import-Csv 'daily_log_computername_08160700.csv'
ERROR: +      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : NotSpecified: (:) [Import-Csv], ExtendedTypeSystemException
ERROR:     + FullyQualifiedErrorId : AlreadyPresentPSMemberInfoInternalCollectionAdd,Microsoft.PowerShell.Commands.ImportCsvCommand
ERROR:
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Import-csv same member

Post by jvierra »

Add a new header then either ignore the first row or remove it.

$csv = Import-Csv <filename> -header column, column2, ... |?{$_.ColumnName -notmatch "\\\\computername\\Process\(APP\)\\Virtual Bytes"}

You have to escape all special characters.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Import-csv same member

Post by sekou2331 »

I need the other columns and there are a lot since this is a perfmon CSV. It is just one or two of them have the same name. Is there any other way to ignore the other duplicate columns.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Import-csv same member

Post by jvierra »

You have to rename all columns in your "header" statement. Rename all with the same name except for the duplicate column. Give that one a new name.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Import-csv same member

Post by jvierra »

You have to rename all columns in your "header" statement. Rename all with the same name except for the duplicate column. Give that one a new name.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Import-csv same member

Post by sekou2331 »

So what I was thinking was using a get-content and do a select on the first line then use a get-unique to remove the dups but it seems the get-unique is not working. Am I missing something?
  1. $a = Get-Content .\daily_log_computername_08160700.csv | select -First 1
  2. $a  | sort | Get-Unique
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Import-csv same member

Post by jvierra »

sekou2331 wrote:So what I was thinking was using a get-content and do a select on the first line then use a get-unique to remove the dups but it seems the get-unique is not working. Am I missing something?
  1. $a = Get-Content .\daily_log_computername_08160700.csv | select -First 1
  2. $a  | sort | Get-Unique
Why do it the hard way, Import-Csv is the correct method. Get-Content is unnecessary.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Import-csv same member

Post by sekou2331 »

I am sorry I am kind of confused. When I try to use anything to do Import-csv I get the error that the member is already present. How do I bypass that?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Import-csv same member

Post by jvierra »

What is the code you used? Are you sure you have a CSV?
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Import-csv same member

Post by sekou2331 »

Sorry for the late replay but below is what I tried and it didn't work. I tried to do a simple skip of the first row like with Get-content. But it still give me the same error.
  1. $a = Import-Csv  .\daily_log_computername_08160700.csv | Select-Object -Skip 1
  2. $a
ERROR: Import-Csv : The member "\\computername\Process(Appname)\Virtual Bytes" is already present.
This topic is 7 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked