Home > Blockchain >  Concatenate two properties inline in select-object
Concatenate two properties inline in select-object

Time:02-08

I have an object array like

{
  Id: 1,
  Name: Name1,
  Users: 
  [
     {
       Identifier: [email protected],
       AccessRight: Admin
     },
     {
       Identifier: [email protected],
       AccessRight: Member
     }
  ]
},
{
  Id: 2,
  Name: Name2,
  Users: 
  [
     {
       Identifier: [email protected],
       AccessRight: Admin
     }
  ]
}

I want to produce the following csv output

Id Name Users
1 Name1 [email protected];Admin,[email protected];Member
2 Name2 [email protected];Admin

I'm failing on concatenating the Users...

$users | Select Id, Name, @{Name="Users";Expression={ $_.Users.Identifier -join "," }} 

produces:

Id Name Users
1 Name1 [email protected],[email protected]
2 Name2 [email protected]

How can I concatenate the Identifier AccessRight with semicolon part before joining with commas?

Many thanks in advance!

CodePudding user response:

Change your property expression to iterate over each object in Users and concatenate the two relevant sub-properties before you -join (again):

 $users | Select Id, Name, @{Name="Users";Expression={ $_.Users.ForEach({$_.Identifier,$_.AccessRight -join ';'}) -join "," }}
  •  Tags:  
  • Related