-1

Let's say I have an array of PSObjects with several properties that are hierarchical. Something like

@{
   'city' = 'CityName'
   'street' = 'StreetName'
   'House' = 'NNN'
   'Apartment' = 'MMM'
}

How do I do nested iteration over these properties? Like, city first, then street etc.?

  1. City A
    • Street a
      • House 1
        • Apartment 1
        • Apartment 2
      • House 2
        • Apartment 1
        • Apartment 2
    • Street b House 1

... etc...

What comes to mind either create lists of unique values for each property and then iterate through them filtering corresponding objects, or use multiple Group-Objects?

miguello
  • 544
  • 5
  • 15
  • Does this answer your question? [Iterate over PSObject properties in PowerShell](https://stackoverflow.com/questions/37688708/iterate-over-psobject-properties-in-powershell) – zett42 Dec 09 '20 at 20:32
  • @zett42 Yeah, I saw that post and no, it doesn't answer. It iterates through all properties, while I need to iterate nested according to hierarchy. I figured this out - see my answer. – miguello Dec 09 '20 at 20:43

1 Answers1

0
foreach($groupCity in $myObject | Group-Object 'city')
   Write-Host($groupCity.Name)
   foreach($groupStreet in $groupCity.Group | Group-Object 'street'){
      Write-Host($groupStreet.Name)
      <#and so on...#>
   }
}
miguello
  • 544
  • 5
  • 15