Вы можете использовать командлет Sort-Object
и некоторый код для получения желаемого результата.
# test data creation
$csv = @'
Invoice_SIMS 2019 Sales Alan
Invoice_SIMS 2018 Sales Alan
Invoice_SIMS 2016 Sales APAC Elizabeth
Invoice_SIMS 2016 Sales Singapore Michele
Invoice_SIMS 2016 Sales Singapore Michele
Invoice_PAS 2020 Sales Europe Abraham
Invoice_PAS 2018 Sales APAC Tan
Invoice_PAS 2017 Sales Singapore Lim
'@ -split [System.Environment]::NewLine
$csv = $csv | ForEach-Object -Process {$_ -split ' {2,}' -join ','}
$csv = ConvertFrom-Csv -InputObject $csv -Header FileName, Year, Department, Owner
# get the name of a temporary file for storing changed test data
$tempcsvfilename = [System.IO.Path]::GetTempFileName()
# modify test data and save it in a temporary file
$curr_filename = ''
$csv | Sort-Object -Property FileName, Year -Descending |
ForEach-Object -Process {
if ( $_.FileName -ne $curr_filename ) {
$curr_filename, $dept, $owner = $_.FileName, $_.Department, $_.Owner
} else {
$_.Department, $_.Owner = $dept, $owner
}
$_
} | Export-Csv -LiteralPath $tempcsvfilename -Encoding UTF8 -NoTypeInformation
# view modified test data
Get-Content -LiteralPath $tempcsvfilename -Encoding UTF8
Вывод:
"FileName","Year","Department","Owner"
"Invoice_SIMS","2019","Sales","Alan"
"Invoice_SIMS","2018","Sales","Alan"
"Invoice_SIMS","2016","Sales","Alan"
"Invoice_SIMS","2016","Sales","Alan"
"Invoice_SIMS","2016","Sales","Alan"
"Invoice_PAS","2020","Sales Europe","Abraham"
"Invoice_PAS","2018","Sales Europe","Abraham"
"Invoice_PAS","2017","Sales Europe","Abraham"