liveBook · Manning

Pipeline parameter binding bypropertyname

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Parameter binding by property name is a PowerShell parameter-binding mode that passes an input object's property value to a command parameter with the same name.

Pipeline parameter binding by property name matches an incoming object's property to a command parameter with the same name. PowerShell then passes the property's value when that parameter accepts pipeline input by property name. section 4.4

Use the technique to:

  • Connect object-producing commands to commands whose parameters expose matching names. section 4.2
  • Bind several properties from one object when corresponding parameters support property-name input. section 4.4
  • Populate command parameters from CSV columns, as with Users.csv flowing into New-ADUser. section 4.4

Trace a fallback binding

Get-Process -Name o* | Stop-Job first attempts ByValue, but a Process cannot become a Job. PowerShell then binds the process object's ID property to Stop-Job's -Id parameter. The binding succeeds technically, yet the command fails because the process ID is not a job ID. Use -WhatIf for safer experiments and Trace-Command -Name ParameterBinding to inspect each decision. section 4.4.1

Import-CSV Users.csv | New-ADUser

Import-CSV Users.csv | New-ADUser demonstrates how matching CSV column names can supply command parameters without manually rewriting every value. section 4.4

When neither binding mode applies, PowerShell does not silently discard the object; it reports that the input cannot bind and stops the pipeline. Design tools with one clear ByValue parameter, while allowing multiple ByPropertyName parameters where names make the mapping unambiguous. section 4.4.2 section 4.4.3

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Explains matching, multiple-property binding, tracing, failure modes, and advanced-function parameter declarations.
Examples
Uses Stop-Job tracing, Import-CSV with New-ADUser, and Get-MachineInfo parameters accepting property-name input.
What it assumes
Assumes PowerShell parameters and pipelines, while reviewing ByValue binding before introducing property-name matching.
What you can do after
Diagnose pipeline binding with Trace-Command and design advanced functions that accept matching object properties.
How it connects
Links pipeline input with ByValue binding, advanced functions, parameter sets, CSV import, and Trace-Command.

Also explained in

Compare how 2 books explain pipeline parameter binding bypropertyname →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.