Why are the two PowerShell "Select Name, Count" statements below (from an array of objects) combining into one result. How can I modify to see two results, grouped at two different levels. Each select is returning the correct data, but they are combined in one common set of headings.
cls
$customObjectsArray = @()
#### build a few objects for to reproduce my issue ####
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo1.odx"
Type = "Orchestration"
Method = "GetText"
}
$customObjectsArray += $customObject
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo1.odx"
Type ="Orchestration"
Method = "GetUser"
}
$customObjectsArray += $customObject
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo2.btm"
Type = "Map"
Method = "GetText"
}
$customObjectsArray += $customObject
$customObject = New-Object -TypeName psobject -Property @{
FileType = "Demo2.btm"
Type = "Map"
Method = "GetConnString"
}
$customObjectsArray += $customObject
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo4.odx"
Type = "Orchestration"
Method = "GetText"
}
$customObjectsArray += $customObject
Write-Host ""
Write-Host "============================"
Write-Host ""
Write-Host "Grouped By MethodName, FileType... Count of Items = $($customObjectsArray.Count)"
$customObjectsArray | Group Method, Type | Sort Count -Descending | Select Name, Count
# I tried this way as well, same result.
#$groupdArray2 = $customObjectsArray | Group Method, Type | Sort Count -Descending
#$result1 = $groupedArray2 | Select Name, Count
#$result1
Write-Host ""
Write-Host "============================"
Write-Host ""
Write-Host "Grouped By MethodName Only ... Count of Items = $($customObjectsArray.Count)"
$customObjectsArray | Group Method | Sort Count -Descending | Select Name, Count
# I tried this way as well, same result.
#$groupedArray1 = $customObjectsArray | Group Method | Sort Count -Descending
#$result2 = $groupedArray1 | Select Name, Count
#$result2
Actual Output:
============================
Grouped By MethodName, FileType... Count of Items = 5
============================
Grouped By MethodName Only ... Count of Items = 5
Name Count
---- -----
GetText, Orchestration 2
GetUser, Orchestration 1
GetText, Map 1
GetConnString, Map 1
GetText 3
GetUser 1
GetConnString 1
Desired Output:
============================
Grouped By MethodName, FileType... Count of Items = 5
Name Count
---- -----
GetText, Orchestration 2
GetUser, Orchestration 1
GetText, Map 1
GetConnString, Map 1
============================
Grouped By MethodName Only ... Count of Items = 5
Name Count
---- -----
GetText 3
GetUser 1
GetConnString 1
--- Updated Answer ---
Getting rid of Select and replacing with Format-Table seems to have cleared up the output. I also got rid of the redundant Write-Host cmds.
I cleaned up your example so each object had the same properties. Then I broke down the Pipe using an intermediate variable and it seems to work fine.
cls
$customObjectsArray = @()
#### build a few objects for to reproduce my issue ####
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo1.odx"
Type = "Orchestration"
Method = "GetText"
}
$customObjectsArray += $customObject
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo1.odx"
Type = "Orchestration"
Method = "GetUser"
}
$customObjectsArray += $customObject
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo9.btm"
Type = "Map"
Method = "GetText"
}
$customObjectsArray += $customObject
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo2.btm"
Type = "Map"
Method = "GetConnString"
}
$customObjectsArray += $customObject
$customObject = New-Object -TypeName psobject -Property @{
FileName = "Demo4.odx"
Type = "Orchestration"
Method = "GetText"
}
$customObjectsArray += $customObject
"`n`n============================`n"
'Grouped By MethodName... Count of Items = $($customObjectsArray.Count)'
$Tally = $customObjectsArray | Group Method
$Tally | Sort Count -Descending | FT Name, Count
"`n`n============================`n"
'Grouped By MethodName, FileType... Count of Items = $($customObjectsArray.Count)'
$Tally2 = $customObjectsArray | Group Method, Type
$Tally2 | Sort Count -Descending | Ft Name, Count
Sample Output:
============================
Grouped By MethodName... Count of Items = $($customObjectsArray.Count)
Name Count
---- -----
GetText 3
GetUser 1
GetConnString 1
============================
Grouped By MethodName, FileType... Count of Items = $($customObjectsArray.Count)
Name Count
---- -----
GetText, Orchestration 2
GetUser, Orchestration 1
GetText, Map 1
GetConnString, Map 1
PS>