According to MSDN:
"Parameters must be declared on public non-static fields or properties. Parameters should be declared on properties. The property must have a public set accessor, and if the ValueFromPipeline or ValueFromPipelineByPropertyName keyword is specified, the property must have a public get accessor."
Why do I have to declare get accessors in my cmdlet ValueFromPipeline parameters? As far as I know, PowerShell only needs to put their values in, not read them out. Thanks (by the way im just curious about this behavior :) ).
PowerShell reads the default value of the parameters marked with ValueFromPipeline or ValueFromPipelineByPropertyName to make a backup before assigning the new value obtained from the pipeline.
Consider the following cmdlets:
New-Post -Title <string>
Set-Post -InputObject <Post> -Title <string>
where the following applies:
New-Post cmdlet returns the newly created Post object to the pipeline, which has a Title propertyInputObject property on the Set-Post cmdlet is marked with ValueFromPipeline = true
Title property on the Set-Post cmdlet is marked with ValueFromPipelineByPropertyName = true.Combining them with the following command:
New-Post -Title "Foo" | Set-Post
and setting a breakpoint on the get accessor of the Set-Post cmdlet's Title property results in the following stack trace:

As you can see, the CmdletParameterBinderController.GetDefaultParameterValue method is invoked during the process of binding the Title property on the Set-Postcmdlet with the value from the corresponding property on the object coming from the pipeline.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With