Using Import-CSV in Powershell, ignoring commented lines

Question:

I think that I must be missing something obvious because I’m trying to use Import-CSV to import CSV files that have commented out lines (always beginning with a # as the first character) at the top of the file, so the file looks like this:

I’d like to ignore those first 5 lines, but still use Import-csv to get the rest of the information nicely in to Powershell.

Thanks

Answer:

CSV has no notion of “comments” – it’s just flat data. You’ll need to use Get-Content and inspect each line. If a line starts with #, ignore it, otherwise process it.

If you’re OK with using a temp file:


Edit: Forgot about convertfrom-csv. It gets much simpler this way.

Source:

Using Import-CSV in Powershell, ignoring commented lines by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply