liveBook · Manning

Positional parameter

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

Positional parameter binding assigns arguments to command parameters according to their declared positions.

Positional parameter binding lets PowerShell assign unnamed argument values according to the declared positions of a command’s parameters. The order of values supplies information that explicit parameter names would otherwise provide. section 4.2

PowerShell resolves command input in a predictable sequence:

  • Binds named command-line arguments first, then positional arguments, and finally checks that mandatory parameters are present. section 4.3.2
  • Performs this command-line binding before considering pipeline binding, so an explicitly supplied parameter can prevent ByValue binding from being considered. section 4.3.2
  • Makes terse commands readable by rewriting omitted parameter names and exposing which parameters receive each value. section 4.2

Make the implicit explicit

A compact pipeline such as `Get-Service | Where-Object –Property Status –eq –Value "Running" | ConvertTo-HTML` exposes the parameters behind the shorter `Get-Service | Where Status –eq "Running" | ConvertTo-HTML` form. Expanding the names shows that `Where-Object` uses `-Property`, an equality operator, and `-Value`; the positional arrangement is no longer mysterious. section 4.2

PS C:\> trace-command -Expression { get-process | convertto-html | 
out-null } -Name ParameterBinding -PSHost 

Trace `ParameterBinding` while running a pipeline to observe named binding, positional binding, mandatory checks, and subsequent pipeline processing. section 4.3.2

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

How deep it goes
Defines positional binding, explains its precedence in binding order, and traces execution without detailing parameter implementation internals.
Examples
Uses Get-Service, Where-Object, ConvertTo-HTML, and Trace-Command examples to expose and trace implicit binding.
What it assumes
Teaches from zero that commands receive input through parameters while assuming familiarity with basic PowerShell command and pipeline syntax.
What you can do after
Diagnose command behavior by expanding omitted parameter names and tracing ParameterBinding activity.
How it connects
Connects positional parameters with command parameters, mandatory parameters, pipeline input, ByValue, and ByPropertyName.

Also explained in

Compare how 5 books explain positional parameter →

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.