byrollo
December 8, 2019, 12:44pm
1
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={$_.id}},
@{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={$_.id}} | Select-Object *,
@{name="MyPropRef"; expression={$_.MyProp}}
Id MyProp MyPropRef
3780 3780 3780
Thanks
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.
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.
Would reference ID property again from the pipeline do the trick?
[pre]
Get-Process | Select-Object Id,
@{name=“MyProp”; expression={$.id}},
@{name=“MyPropRef”; expression={$ .id}}
[/pre]