Select-Object on nested collections

Your can use -ExpandProperty parameter of Select-Object cmdlet. It will expand collection and add selected properties from parent object to child objects:

$changesets | Select-Object ChangeSetId, Owner, Comment,
    @{Name="Changes"; Expression={ $_.Changes | Select-Object ChangeType, ServerItem }} |
Select-Object -Property * -ExcludeProperty Changes -ExpandProperty Changes

Your expression syntax isn't quite right, you need an equals ("=") between expression and the code block.

$changesets | Select-Object `
ChangeSetId,
Owner,
Comment,
@{Name="Changes"; Expression={ $_.Changes | Select-Object ChangeType, ServerItem }}

If you want the nested object to be a top level elements going forward you could also do...

$changesets | Select-Object `
ChangeSetId,
Owner,
Comment,
@{Name="ChangeType"; Expression={ $_.Changes.ChangeType}},
@{Name="ServerItem"; Expression={ $_.Changes.ServerItem }}

I would probably unroll the inner collection and output once for each item in the inner collection. Something like this (not tested):

$changesets | foreach-object {
  $changeSetItem = $_
  $changeSetItem.Changes | foreach-object {
    $changeItem = $_
    new-object PSObject -property @{
      "ChangeSetId" = $changeSetItem.ChangeSetId
      "Owner" = $changeSetItem.Owner
      "Comment" = $changeSetItem.Comment
      "ChangeType" = $changeItem.ChangeType
      "ServerItem" = $changeItem.ServerItem
    }
  }
}

Bill

Tags:

Tfs

Powershell