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
ValueFromPipelineorValueFromPipelineByPropertyNameto make a backup before assigning the new value obtained from the pipeline.Consider the following cmdlets:
where the following applies:
New-Postcmdlet returns the newly createdPostobject to the pipeline, which has aTitlepropertyInputObjectproperty on theSet-Postcmdlet is marked withValueFromPipeline = trueTitleproperty on theSet-Postcmdlet is marked withValueFromPipelineByPropertyName = true.Combining them with the following command:
and setting a breakpoint on the get accessor of the
Set-Postcmdlet’sTitleproperty results in the following stack trace:As you can see, the
CmdletParameterBinderController.GetDefaultParameterValuemethod is invoked during the process of binding theTitleproperty on theSet-Postcmdlet with the value from the corresponding property on the object coming from the pipeline.