Are you looking for this:
(Import-Csv -path .\testing.csv | Select-Object -expandproperty Salary | Measure -Sum).sum
More about Expandproperty Here
题
Below is column imported from CSV file. Measure is not able to calculate sum, and others operations on it. As you can see I tried data type conversion, but still Measure won't calculate sum. Salary column may have blanks and zeros. Is there one liner solution for this?
Salary
------
120
220
450
620
780
0
This is info from power shell
# measure just gives count, but not sum, max, min
# count in string = 7 (0 and blank included)
# count in int32 = 5
# Input object "" is not numeric.
Import-Csv -path .\testing.csv | Where-Object {$_.Salary -as [Int32]} | Select-Object Salary | Measure -Sum
I also tried below, code. It gave output in currency format, and I feel, which is numeric, and measure can sum, average it, but it still failed.
$objFile | ForEach-Object { $_.Salary.ToString("C2") } | Measure
Also, tried below, which ensured that Salary was Int, but still measure won't sum it.
$objFile = Import-Csv -Path .\testing.csv | Select @{Name="Salary"; Expression={[int32]$_.Salry}}
$objFile |GM
#Measure-Object : Input object "" is not numeric.
解决方案
Are you looking for this:
(Import-Csv -path .\testing.csv | Select-Object -expandproperty Salary | Measure -Sum).sum
More about Expandproperty Here