2

I have a csv file with the following values. Use import-csv to calculate the highest version for each doc.

DocName      Version
-----------------
DocA.docx     4.0
DocA.docx     3.9
DocA.docx     3.8
DocB.docx     2.0
DocB.docx     1.0
DocC.docx     0.2
DocC.docx     0.1

How can I get the maximum version for each DocName? Example, for DocA.docx , the highest version is 4.0 and for the docB.docx , the highest version is 2.0.

I need to give the output using powershell?

3 Answers 3

1

Group table, sort versions and select the highest number

$TableGrouped = $Table | Group-Object {$_.DocName}

ForEach ($item in $TableGrouped) {
    $item.Group | Sort-Object -Descending -Property Version | Select-Object -First 1 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer as well, i have test this as well...it is working..Thank you Rob
0

I recommend to treat (compare) versions as [version] types rather than the default Import-Csv [string] type or a [decimal] type as suggested by @Dave.
To this in one go, you might use a [version[]] array in the calculated property:

Import-Csv .\MyFile.csv | Group-Object DocName |
    Select Name, @{
        Name = 'Version'
        Expression = {([version[]]$_.Group.Version | Measure-Object -Maximum).Maximum}
    }

Comments

0

When you import the CSV you will need to convert the Version to a decimal, then you can use the Group-Object and Measure-Object to get the highest Version by file. Something like this:

$file = Import-Csv -Delimiter ',' | % {$_.Version = [decimal]$_.Version} 
$file | Group-Object DocName | Select Name, @{Name = 'Version'; Expression = {($_.Group.Version | Measure-Object -Maximum).Maximum}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.