I'm using Powershell 5 if that's relevant
When adding a property to the current item in a pipeline:
I cannot refer to $_ until I have fed it into another pipeline. Why?
# make some objects to pass around
$test = (0..3) | %{[pscustomobject]@{key1="v$_.1";key2="v$_.2"} }
# GM = TypeName: System.Management.Automation.PSCustomObject
# trying to use the current pipeline object
$test | Add-Member @{key3=$_.key1}
$test | ft *
key1 key2 key3
---- ---- ----
v0.1 v0.2
v1.1 v1.2
v2.1 v2.2
v3.1 v3.2
# try again, this time taking the current pipeline object...
#... and then putting it into another identical looking pipeline
$test | %{ $_ | Add-Member @{key4=$_.key1} }
$test | ft *
key1 key2 key3 key4
---- ---- ---- ----
v0.1 v0.2 v0.1
v1.1 v1.2 v1.1
v2.1 v2.2 v2.1
v3.1 v3.2 v3.1
I suspect it might be that the first one is implying something automatically and I haven't told the invisible/implied function to pass on $PSItem.
[pscustomobject]@{ key1='something else' } | % { $test | Add-Member @{key3=$_.key1} }.