0

I have a pscustomobject which i want to remove items from. Every entry contains 3 values separated by a semicolon. The first one is a code, the second one a date, and the third one a description. I would like to make either a new pscustomobject containing only the most up to date ones (marked by *), or filter all the older entries from this object. Any help ?

$allocationData = get-content $Global:costAllocationFile | sort

942200;20170701;Merk & Design
942200;20171106;Merk & Design
942200;20171207;Merk & Design *
942800;20170102;Formule Management
942800;20170327;Formule Management *
943120;20170102;Unit 4
943120;20170911;Unit 4 & retail
943120;20171207;Unit 4
943120;20180402;Unit 4
943120;20180703;Unit 4 *
943300;20170102;Inkoop
943300;20170130;Inkoop
943300;20170717;Inkoop *

edit : I forgot to mention, the descriptions may change over time, explaining my motivation

mklement0
  • 382,024
  • 64
  • 607
  • 775
Ivosein
  • 53
  • 5

1 Answers1

2

Use Import-Csv instead of Get-Content

Import-Csv $Global:costAllocationFile -Delimiter ';' -Header Code,Date,Description 

And Group-Object code to then sort by Date inside the group and just select the last one

Import-Csv $Global:costAllocationFile -Delimiter ';' -Header Code,Date,Description |
  Group-Object Code | Foreach-Object {
    $_.Group  | Sort-Object Date | Select-Object -Last 1
  }

Sample output:

Code   Date     Description
----   ----     -----------
942200 20171207 Merk & Design *
942800 20170327 Formule Management *
943120 20180703 Unit 4 *
943300 20170717 Inkoop *