Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select-Object on nested collections

Tags:

powershell

tfs

I'm writing a Powershell cmdlet to list changesets from TFS. I successfully query TFS and get a collection of changesets but want to return simplified objects that contain only a few properties. I can do that using Select-Object like this...

$changesets | Select-Object ChangeSetId, Owner, Comment

The last property I would like to add is the Changes property which is an array of changes. I would like to simplify those objects as well. I'm trying this but it doesn't return what I want...

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

Is there a way to handle nested collections with Select-Object?

like image 816
codeConcussion Avatar asked Apr 04 '13 16:04

codeConcussion


People also ask

How do I find the value of a nested object?

How do I find the value of a nested object? If you need to search for a nested object, you can use Lodash's . find() function.It takes three arguments: collection : which can be either an array or object.

How to Select-object in PowerShell?

The Select-Object cmdlet selects specified properties of an object or set of objects. It can also select unique objects, a specified number of objects, or objects in a specified position in an array. To select objects from a collection, use the First, Last, Unique, Skip, and Index parameters.


1 Answers

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
like image 108
user4003407 Avatar answered Oct 16 '22 21:10

user4003407