Search code examples
powershellpowershell-4.0

How to get maximum value using PowerShell?


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?


Solution

  • 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}
        }