0

I am not sure what is wrong with the script. I have a 3 column csv file that consists of Name,Count,Owner. The goal is to get the highest count of each name, then output all 3 columns, however the Owner column is not outputting. I am only getting Name, Total. I would appreciate if someone would assist in showing me what is wrong, many thanks.

$total = Import-Csv -Delimiter ',' -Path "file.csv"

$total = $total | Group-object -Property Name | Select Name,  @{ N = 'Total';E = {($_.Group | Measure-Object -Property count -Maximum).Maximum }},Owner

Contents of csv file:

"Name","Count","Owner"
"ctx_Prd-DG","1","User1"
"PRD-Fa","5","User2"
"ING-PROD","3","User2"
"PROD-DG03","0","User2"
"PROD-DG01","0","User2"
"PRD-2018-DG","1","User3"
"PRD-7-DG","5","User3"
"PRD-7-DG-PR15","0","User3"
"PRD-CS-DG","0","User3"
"PRD-INSIGHT-DG","0","User3"
"PRD-LIVE-DG","0","User3"
"DC01-DG","0","User4"
"Test - DG","0","User4"
"PRD-CS-DG","0","User3"
"INSIGHT-DG","0","User3"
"ctx_Prd-DG","1","User1"
"PRD-Fa","1","User2"
"ING-PROD","0","User2"
"PROD-DG03","0","User2"
"PROD-DG01","0","User2"
"PRD-2018-DG","7","User3"
"PRD-7-DG","5","User3"
"PRD-7-DG-PR15","0","User3"
"PRD-CS-DG","0","User3"
"PRD-INSIGHT-DG","0","User3"
"PRD-LIVE-DG","2","User3"
"DC01-DG","1","User4"
"Test - DG","8","User4"
"PRD-CS-DG","20","User3"
"INSIGHT-DG","0","User3"
Palo
  • 804
  • 10
  • 22

2 Answers2

0

Have you considered this option?

$total = Import-Csv -Path file.csv
$grouped = $total | Group-Object -Property Name
$output = $grouped | Select-Object Name, @{ N = 'Total'; E = {$_.Count} }, @{ N='Owner'; E= {($_.Group).Owner}}

Regarding the "Name,Expression" syntax, here is a helpful question posted in SO.

Select-Object expression

Alex_P
  • 1,518
  • 2
  • 12
  • 24
  • Thanks, I tried the suggestions, but still the Owner column is being eliminated. It seems like this part here is removing the "Owner" column even if I specifically include it in the import and export. @{ N = 'Total';E = {($_.Group | Measure-Object -Property count -Maximum).Maximum }} – user12086324 Jun 07 '20 at 16:10
  • @user12086324, I corrected the code snippet. You can try again. I realized I did not attentively read your question. – Alex_P Jun 07 '20 at 16:24
  • @user12086324, Can you provide an example what you mean with maximum? – Alex_P Jun 07 '20 at 16:43
  • I need to get the maximum for each grouped Name, this works, But the Owner column is not exporting..... $total | Group -Property Name | Select Name, @{ N = 'Total';E = {($_.Group | Measure-Object -Property count -Maximum).Maximum } } , Owner – user12086324 Jun 07 '20 at 16:43
0

Simply $total | Group-Object -Property Name does not have a property "Owner"

# `Get-Member -MemberType Properties` shows properties of an object.
$group = $total | Group-Object -Property Name
$group | Get-Member -MemberType Properties

# See how `Group-Object` groups each item,
$group[1].Group
$group[2].Group

So your code will be like the following

$csv =  Import-Csv "file.csv"
$total = $csv | Group-Object -Property Name | Select Name,  @{
  N = 'Total'
  E = { ($_.Group | Measure-Object -Property count -Maximum).Maximum }
}, @{
  N = 'Owner'
  E = { $_.Group.Owner[0] }
}
7cc
  • 1,099
  • 2
  • 10
  • Count NoteProperty System.Double Count=3 Name NoteProperty System.String Name=Prd-DG Owner NoteProperty System.String Owner=User1 – user12086324 Jun 08 '20 at 14:51