Home > Software design >  Sort of Excel files
Sort of Excel files

Time:07-07

I am having the script to sort the excel data to descending order format and I need to save the data to another location. But while executing the below script error is showing.

Import-Csv -Path D:\Excel\details.csv | Sort-Object AMCore Content Version | Export-Csv D:\sb.csv

CSV Input data

Output

CodePudding user response:

The error comes from an unquoted argument, Sort-Object is binding AMCore positionally as argument for the -Property parameter and has no clue on what to do with Content as the next argument.

... | Sort-Object 'AMCore Content Version' | ...

The above will be syntactically correct, however that's not enough to do a proper sorting on these Values, it would require an expression script block which would convert each value to either decimal or double to do proper sorting:

Import-Csv -Path D:\Excel\details.csv |
    Sort-Object { [double] $_.'AMCore Content Version' } |
        Export-Csv D:\sb.csv -NoTypeInformation

You can find an example below for testing:

$details = @'
AMCore Content Version
2.5
1.5
1
0.5
'@ | ConvertFrom-Csv

$details | Sort-Object { [double] $_.'AMCore Content Version' }
  • Related