Created
July 13, 2020 19:48
-
-
Save sheldonhull/af7e3355953a2f6533c813d9ca220a7d to your computer and use it in GitHub Desktop.
[Aggregate PowerShell Objects] How to use measuring and grouping to calculate the group counts of a powershell object. This can be useful for preaggregation of totals before exporting to excel for example #powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Companies = @( | |
[pscustomobject]@{CompanyName = 'Foo'; UserCount = 5 } | |
[pscustomobject]@{CompanyName = 'Foo1'; UserCount = 5 } | |
[pscustomobject]@{CompanyName = 'Foo'; UserCount = 2 } | |
[pscustomobject]@{CompanyName = 'Foo'; UserCount = 3 } | |
) | |
$CalculatedResults = $Companies | Group-Object CompanyName | ForEach-Object { | |
$i = $_ | |
[int]$totalusers = ($i.Group | Measure-Object -Sum -Property UserCount).Sum | |
[pscustomobject]@{ | |
CompanyName = $i.Name | |
TotalUsers = $totalUsers | |
} | |
} | |
Write-Host "#####################" | |
Write-Host "Unaggregated Results" | |
Write-Host "$($Companies | Format-Table -Autosize -Wrap | Out-String)" | |
Write-Host "#####################" | |
Write-Host "`n`n`n`n" | |
Write-Host "#####################" | |
Write-Host "Calculated Results" | |
Write-Host "$($CalculatedResults | Format-Table -AutoSize -Wrap | Out-String)" | |
Write-Host "#####################" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment