Share via


Remove first row of CSV, then select column headers

Question

Sunday, June 23, 2019 12:19 AM

Hi there,

I have a CSV that looks like this:

"Internal - All"
"Address","Content","Status Code"
"https://dog.com", "text/html", "200"
"https://dog.com/page/", "text/html", "200"

etc.

I'm an utter noobie to PowerShell. I'm trying to write a query that takes this CSV and:

  1. Imports the CSV
  2. Removes the row "Internal - ALL"
  3. Selects just the Address and Content columns
  4. Prints the new version of the CSV

I was able to do 1,3,4 after manually deleting the first row with:

import-csv C:\file.csv |   select Address,Content | Export-Csv -Path C:\destination\file.csv –NoTypeInformation

And I've seen people suggest using "Select -skip 1" to get rid of the first row, but that isn't working for me.

Can anyone suggest the syntax I'd need to solve this problem?

Thanks~

All replies (7)

Sunday, June 23, 2019 1:34 AM ✅Answered

Please format your code as  code using the code posting tool provided on the icon bar of the post editor (second to last icon). Thanks.

Because the first row does not belong to a valid CSV file or does not fit to the rest of your CSV file you should treat it as text file and use Get-Content first.

Get-Content -Path C:\file.csv |
    Select-Object -Skip 1 |
        ConvertFrom-Csv |
            Select-Object -Property Address,Content |
                Export-Csv -Path C:\destination\file.csv –NoTypeInformation

Live long and prosper!

(79,108,97,102|%{[char]$_})-join''


Sunday, June 23, 2019 2:03 AM

get-content in.csv |
   select -skip 1 |
convertfrom-csv |
   select address, content |
Export-csv out.csv –NoTypeInformation

Keith


Sunday, June 23, 2019 2:56 AM

Get-Content -Path C:\tester.csv 
    | Select-Object -Skip 1
        | ConvertFrom-Csv
           | Select-Object -Property Address,Content,Title 1, `(bytes`)
              | Export-Csv -Path C:\testerResult.csv –NoTypeInformation

Hey there,

I'm now getting the following error when attempting to use the syntax you described above.

Is there a syntax error I'm missing? Maybe an issue with the escaped characters or white spaces? The error is:

Select-Object : 
A positional parameter cannot be found that accepts argument 
'System.Object[]'.
At line:1 char:106
+ ... tFrom-Csv | Select -Property Address,Content,Title 1, ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterBindin 
   gException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Comm 
   ands.SelectObjectCommand

Thanks for your help!


Sunday, June 23, 2019 3:00 AM

Nevermind! I wrapped the arguments with white spaces and parenthesis in quotes after searching a bit and the script worked. Thank you!


Sunday, June 23, 2019 3:00 AM

Your so called CSV is not a CSV.  It has one header and two columns.  THat can never work.

Please use you search provider to look up what a CSV is.

\(ツ)_/


Sunday, June 23, 2019 6:13 AM | 1 vote

Of course the actual file is not a single row and two columns. I truncated the actual number of columns and rows (using spreadsheet vernacular for the sake of simplicity) for the sake of a sample question. If someone is asking a PowerShell question, I feel it's quite belittling to imply that they 'don't know what a CSV is,' no? And quite a discouraging symbolic tone for one to attach to one who I'd assume, as PowerShell forum moderator, is representative of the community.

Luckily, the users who so kindly answered seemed to understand what I was getting at just fine.


Sunday, June 23, 2019 6:24 AM | 1 vote

Two thirds of the people posting questions about a CSV do not know what a CSV is.

Your sample is useless because it is not a legal CSV.

The post you marked as an answer also indicates that you don't know what a CSV is.  The suggestion is telling you that you don't have a real Csv.  If it works then your file may be partially useful.   The fact that your header is wrong and that you may be able to use a custom header also tells us that you do not have a CS file.

Perhaps you should still do the requisite research.

\(ツ)_/