user2363207
user2363207

Reputation:

PowerShell - Show Property of Parent Object and Child Object

If I have a list of email metadata in a hashtable and each email has a hashtable with a list of attachments inside that object, like this:

$Emails = @{     
            ID          = "E123"; 
            Subject     = "Check this out"; 
            Attachments = @{
                            ID   = "A123"; 
                            Name = "FunnyPic.jpg"
                           }
           }

And then that hashtable is converted to a PSObject like this:

$EmailsObject = New-Object -TypeName PSObject -Property $Emails

And I want to do something like this:

$EmailsObject | Select ID, Attachments.ID

How would I do that?

I want to associate the ID of the Email with the ID's of the associated attachments.

Upvotes: 5

Views: 2905

Answers (2)

hsimah
hsimah

Reputation: 1303

You will want to use calulated properties.

$Emails | Select-Object -Property @{Name='ID';Expression={$_.ID};},@{Name='AttachmentId';Expression={$_.Attachments.ID};}

You can shorten Name and Expression to n and e.

See this article for more.

Upvotes: 3

Lee_Dailey
Lee_Dailey

Reputation: 7489

you need to use a calculated property if you use Select-Object. something like this ...

$Emails = @{     
            ID          = "E123"; 
            Subject     = "Check this out"; 
            Attachments = @{
                            ID   = "A123"; 
                            Name = "FunnyPic.jpg"
                           }
           }
$EmailsObject = New-Object -TypeName PSObject -Property $Emails

$EmailsObject |
    Select-Object ID,
        @{
        n = 'AttID'
        e = {$_.Attachments.ID}
        }

output ...

ID   AttID
--   -----
E123 A123

Upvotes: 0

Related Questions