title: Powershell - Import CSV sort on Date and assign earliest date for each user ID
body: <p>Trying to import CSV that has columns of ID, Start Date, End Date where each row could contain the same ID value but different dates.<br />
  Want to sort this so it's finding the earliest date for each ID <strong>AND</strong> the latest End date for that same ID, then write that to a CSV.</p>
  <p>Input csv:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">ID</th>
  <th style="text-align: center;">Start Date</th>
  <th style="text-align: right;">End Date</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">100</td>
  <td style="text-align: center;">01-Apr-2021</td>
  <td style="text-align: right;">23-May-2021</td>
  </tr>
  <tr>
  <td style="text-align: left;">100</td>
  <td style="text-align: center;">10-June-2021</td>
  <td style="text-align: right;">30-Dec-2021</td>
  </tr>
  <tr>
  <td style="text-align: left;">200</td>
  <td style="text-align: center;">12-Aug-2021</td>
  <td style="text-align: right;">23-Sep-2021</td>
  </tr>
  <tr>
  <td style="text-align: left;">200</td>
  <td style="text-align: center;">25-Dec-2021</td>
  <td style="text-align: right;">24-May-2022</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Output csv:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">ID</th>
  <th style="text-align: center;">Start Date</th>
  <th style="text-align: center;">End Date</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">100</td>
  <td style="text-align: center;">01-Apr-2021</td>
  <td style="text-align: center;">30-Dec-2021</td>
  </tr>
  <tr>
  <td style="text-align: left;">200</td>
  <td style="text-align: center;">12-Aug-2021</td>
  <td style="text-align: center;">24-May-2022</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I thought I could import CSV, then identify ID and then for each ID sort start and end dates but can't seem to get it to work.</p>
  <p>Code thus far:</p>
  <pre><code>$source = &quot;C:\test\test.csv&quot;
  $outPut = &quot;C:\test\Testsort.csv&quot;
  
  $csv = Import-Csv $source 
  Foreach ($usr in $csv){
      $userID= $usr.&quot;ID&quot;
      $filtered = $csv | Where-Object {($_.'ID') -eq $userID}
      $output = [pscustomobject]@{
                       'ID'  = $userID
                       'Start Date'   =($filtered.'Start Date' | Sort-Object @{Expression= 
                        {[datetime]($_.&quot;Start Date&quot;)};Descending=$false} | Select -First 1
                       'End Date'     =($filtered.'Start Date' | Sort-Object @{Expression= 
                        {[datetime]($_.&quot;End Date&quot;)};Descending=$True} | Select -Last 1
                       }
  }
  $output | Export-csv $outPut 
  </code></pre>
  <p>Sadly I'm struggling with the sorting of start and end dates per ID and setting a value of earliest and oldest per ID.</p>
  <p>Any thoughts?</p>