Select CSV columns in Powershell where header name contains a specific string

Question:

I have a data file of about 10-15 columns from which I want to extract specific columns. Some of the columns I know the exact column header and others I only know that the first two letters will always be “FC”.
How do I select only the columns where I know the column header and those that start with “FC”?
Starting with just the “FC” columns, I have tried like this:

But I just get an error:

Then, if I try:

I get the output I want except that it is in “column header : value” format, like this:

I am probably just missing something simple, but how do I get to the point that I am able to select these matching columns and then output them to another .txt file (without the header : value format)?

Answer:

First things first: Mathias R. Jessen’s helpful tip not only solves your problem, but significantly simplifies the approach (and also works in PSv2):

The (implied) -Property parameter supports wildcard expressions, so FC* matches all property (column names) that start with FC.

As for the output format you’re seeing: Because you’re selecting 5 properties, PowerShell defaults to implicit Format-List formatting, with each property name-value pair on its own line.

To fix this display problem, pipe to Format-Table explicitly (which is what PowerShell would do implicitly if you had selected 4 or fewer properties):

To re-export the results to a CSV (TSV) file:

To do so without a header line:


As for your specific symptom:

The problem occurs only in PSv2, and it smells like a bug to me.

The workaround is make your column-name array a strongly typed string array ([string[]]):

Note that, for brevity, I’ve used built-in alias % in lieu of ForEach-Object and ? in lieu of Where-Object.
Also note that the regex passed to -match was changed to ^FC to ensure that only columns that start with FC are matched.


Your code works as-is in PSv3+, but can be simplified:

Note how .Name is applied directly to .psobject.Properties, which in v3+ causes the .Name member to be invoked on each item of the collection, a feature called member-access enumeration.

Source:

Select CSV columns in Powershell where header name contains a specific string by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply