Hi I need to read a csv file in Powershell and select certain columns and save it into new file.
here is my code
# reading the file
$import = Import-Csv -Path ($file_dir+"Myfile.csv")
# listing out all columns in csv
$import_columnname = $import | Get-member -MemberType 'NoteProperty' | Select-Object -ExpandProperty
'Name'
# grab all columns that I need, using pattern matching
$import_columnneeded = $import_columnname | select-string -Pattern 'stringIdonotNeed|stringIdonotNeed2' -NotMatch
# select the one i need
$import | select -ExpandProperty $import_columnneeded | Export-CSV '.\whereever'
$import_columnneeded is a array, it does not work with | select -ExpandProperty, select -ExpandProperty only works strings. So what can I do to select columns that I need dynamically?
-Propertyand/or theExcludePropertyparameters of theSelect-Objectcmdlet?-ExpandPropertyreturns an array of values for the expanded property. It seems you want to output multiple columns of values. How would you join/delimit the multiple columns of values? If all of this property expansion is required, you may reconsider the schema of the original CSV file.