How to sum only 2nd column values using powershell?
How to sum only 2nd column values using powershell?
To sum the sizes do this:
$totalSize = ($report | Measure-Object Size(MB) -Sum).Sum
$total = 0
$report | % {[float]$tmp = $_.Size(MB).TrimEnd( MB); $total += $tmp}
Then you can just add the $total
object to your custom $report
object before you convertTo-html and bango. Thanks for the neat script.
Only thing that confused me a little was your () your size property of $report makes PS think its a method, thus the quotes. Is not the best convention, but it works.
More explicitly :
...
[array]$report += $obj
}
$total = 0
$report | % {[float]$tmp = $_.Size(MB).TrimEnd( MB); $total += $tmp}
$obj = new-object psobject
add-member -inp $obj noteproperty Path Total Size:
add-member -inp $obj noteproperty Size(MB) $total
[array]$report += $obj
#display the table
...
Also, Remove | Sort Size -Descending
for the total to appear on the bottom.
How to sum only 2nd column values using powershell?
I had a similar need and here is my simple solution:
$stuff=get-stuff
$results=
foreach ($item in $stuff) {
$item | select column1,@{N=Column 2;E={$_.column2}},description,created,@{N=Size (GB);E={{0:N2} -f $_.sizegb}}
}
$results_sorted=@($results | sort created)
$results_sorted+=$item | select @{N=column1;E={Totals}},@{N=Column 2;E={$null}},@{N=Description;E={$null}},@{N=Created;E={$null}},@{N=Size (GB);E={($results | Measure-Object size (gb) -Sum).Sum}}
$results_sorted | ft column1,Column 2,Description,Created,Size (GB)