Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Concatenate two properties inline in select-object

I have an object array like

{
  Id: 1,
  Name: Name1,
  Users: 
  [
     {
       Identifier: a@a.com,
       AccessRight: Admin
     },
     {
       Identifier: b@b.com,
       AccessRight: Member
     }
  ]
},
{
  Id: 2,
  Name: Name2,
  Users: 
  [
     {
       Identifier: c@c.com,
       AccessRight: Admin
     }
  ]
}

I want to produce the following csv output

Id Name Users
1 Name1 a@a.com;Admin,b@b.com;Member
2 Name2 c@c.com;Admin

I’m failing on concatenating the Users…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

produces:

Id Name Users
1 Name1 a@a.com,b@b.com
2 Name2 c@c.com

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

Many thanks in advance!

>Solution :

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 "," }}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading