refer to custom expression property on same select-object

Welcome Forums General PowerShell Q&A refer to custom expression property on same select-object

Viewing 4 reply threads
  • Author
    • #192130
      Topics: 1
      Replies: 0
      Points: 12
      Rank: Member

      can you help me with this simple example script?
      How do I refer to "custom expression property" on single one "select-object"?

      I would like: **single "select-object"**

      Get-Process | Select-Object Id,
      @{name="MyProp"; expression={$}},
      @{name="MyPropRef"; expression={$_.MyProp}}

      ...but third property "MyPropRef" is not displayed!

      Id MyProp MyPropRef
      — —— ———
      3780 3780

      While with *double select-object piped*, "MyPropRef" is displayed

      Get-Process | Select-Object Id,
      @{name="MyProp"; expression={$}} | Select-Object *,
      @{name="MyPropRef"; expression={$_.MyProp}}

      Id MyProp MyPropRef
      — —— ———
      3780 3780 3780


    • #192151
      Topics: 10
      Replies: 118
      Points: 505
      Helping Hand
      Rank: Major Contributor

      What is the actual property name and/or data you're expecting?

      To find the correct property name, do Get-Process | Get-Member and see what the actual property name is.

      And depending on the property, sometimes it may not have anything to output, hence the blank value.

    • #192172
      Senior Moderator
      Topics: 8
      Replies: 1154
      Points: 4,012
      Helping Hand
      Rank: Community Hero

      They are called as calcluated properties.

      Here you are using ID as MYProp and MyPropRef, hence you can use the same expression for both Calculated property. You will have to sue multiple select object if you want to create a calculated property from another calculated property without the first one being created its not available for the second expression.

    • #192190
      Topics: 2
      Replies: 1697
      Points: 3,385
      Helping Hand
      Rank: Community Hero
    • #192358
      Topics: 0
      Replies: 8
      Points: 103
      Rank: Participant

      Would reference ID property again from the pipeline do the trick?

      Get-Process | Select-Object Id,
      @{name="MyProp"; expression={$}},
      @{name="MyPropRef"; expression={$}}
Viewing 4 reply threads
  • You must be logged in to reply to this topic.