title: Sort and export-CSV
body: <p>I have a csv file containing rows of the following extract:</p>
  
  <blockquote>
    <p>"EmployeeID","FirstName","LastName","Location","Department","TelephoneNo","Email"<br>
    "000001    ","abc           ","def                       ","Loc1","    ","              ","name1@company.com                                  "<br>
    "000023    ","ghi           ","jkl                       ","Loc2","  ","              ","name2@company.com                                  "<br>
    "000089    ","mno           ","pqr                         ","Loc2","               ","              ","name3@company.com         "</p>
  </blockquote>
  
  <p>How do I keep the quotes and sort and save as a csv file?</p>
  
  <p>I have the following powershell source script which works with csv files not having double quotes for the columns:</p>
  
  <pre><code>Get-Content $Source -ReadCount 1000 |
      ConvertFrom-Csv -Delimiter $Delimiter | 
      Sort-Object -Property $NamesOfColumns -Unique |
      ForEach-Object {
          # Each of the values in $ColumnValueFormat must be executed to get the property from the loop variable ($_). 
          $values = foreach ($value in $ColumnValueFormat) {
              Invoke-Expression $value
          }
          # Then the values can be passed in as an argument for the format operator.
          $ShowColsByNumber -f $values
      } | 
      Add-Content $Destination;
  </code></pre>
  
  <p>The <code>$Source</code>, <code>$Delimiter</code>, <code>$NamesOfColumns</code> and <code>$ColumnValueFormat</code> are given or built dynamically.</p>
  
  <p><code>$ColumnValueFormat</code> with a non quoted csv file contains:</p>
  
  <pre><code>$_.EmployeeID.Trim()
  $_.FirstName.Trim()
  $_.LastName.Trim()
  $_.Location.Trim()
  $_.Department.Trim()
  $_.TelephoneNo.Trim()
  $_.Email.Trim()
  </code></pre>
  
  <p><code>$ColumnValueFormat</code> with a quoted csv file contains:</p>
  
  <pre><code>$_."EmployeeID".Trim()
  $_."FirstName".Trim()
  $_."LastName".Trim()
  $_."Location".Trim()
  $_."Department".Trim()
  $_."TelephoneNo".Trim()
  $_."Email".Trim()
  </code></pre>
  
  <p>The problem seems to be based around the <code>$ColumnValueFormat</code> that is placing the column headers with the double quotes. (If I remove them I am not sure the internals of the cmdlet will recognize the column headings when it is processing the rows)</p>
  
  <p>I am having two problems:</p>
  
  <ol>
  <li>The column heading surrounded by the double quotes. The problem seems to be based around the <code>$ColumnValueFormat</code> that is placing the column headers with the double quotes as it does not process the rows. (If I remove the double quotes then it does not recognize the column headings when it is processing the rows).</li>
  <li>Another problem I came across last minute is if the last column is blank it thinks it's a null and when the <code>Invoke-Expression $value</code> executes (where <code>$value</code> holds the last column expression of <code>$_.Email.Trim()</code> - on a non quoted CSV file) it bombs. If I try to place the statement in a try/catch block it simply ignore it the last column is not added to the <code>$values</code> array and again bombs.</li>
  </ol>