title: PowerShell: ConvertTo-Csv and then Set-Content by object filter
body: <p>I have the following variables:</p>
  
  <pre><code>$input = "H:\input_file.txt"
  $output = "H:\output_file.txt"
  $data = Get-Content -Path $input | ConvertFrom-Csv -Delimiter '|' -Header 'Col1','Col2','Col3','Col4','Col5'
  </code></pre>
  
  <p>As seen above, the input file has 5 columns.  This file has various record types in it.  Not all record types have 5 columns defined.  So, let's say I have 3 record types -- A, B, and C.  A has 3 columns, B has 4 columns, and C has 5 columns.  An example input file looks like:</p>
  
  <pre><code>A|x|1
  B|y|2|stuff
  C|z|3|stuff|other
  B|y|3|other
  A|z|2
  </code></pre>
  
  <p>My script then makes some modifications to the values in some of the columns (except for Col1) in $data.  I want to output all rows in $data to a text file.</p>
  
  <p>If I do something like</p>
  
  <pre><code>$data | Select Col1,Col2,Col3,Col4,Col5 | ConvertTo-Csv -Delimiter '|' -NoTypeInformation | % {$_ -replace '"', ""} | Select-Object -Skip 1 | Set-Content -Path $output
  </code></pre>
  
  <p>it will append unnecessary pipe characters to record types A and B (because they have less than 5 columns, and yet I am doing <code>Select Col1,Col2,Col3,Col4,Col5</code>).</p>
  
  <p>Is there a clean way to output $data to a text file without the unnecessary pipe characters on record types A and B?  My best guess at the moment is to have 3 separate pipelines for record types A, B, and C, such that I am doing the correct Select for the given record type, and then gluing them all together somehow.</p>