liveBook · Manning

Positional parameter

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

A positional parameter is a command or script parameter whose value can be supplied by its position without naming the parameter explicitly.

A positional parameter accepts a value by its place in a command, so you can omit the parameter name. The value must appear in the correct position. section 3.5.3 section 4.6.3

Use help to identify positional parameters and avoid common mistakes:

  • In a syntax summary, square brackets around the parameter name identify a positional parameter, such as [[-Path] <string[]>]. section 3.5.3
  • Full help shows the position number; position 0 is the first value after the cmdlet. section 4.6.3
  • Put positional values first and in order, then add named parameters such as -Filter. section 4.6.3
  • Prefer named parameters in scripts and other commands that will persist; positional syntax is best reserved for quick interactive commands. section 4.6.3 section 19.2

Passing a path positionally

Get-ChildItem /Users is equivalent to Get-ChildItem -Path /Users because -Path occupies position 0. Reordering unnamed values can make a command fail, while named parameters can appear in a different order. section 4.6.3

Parameterized script

A Param() block makes script parameters both named and positional. For example, .\Get-DiskInventory.ps1 SRV-02 supplies the computer name positionally, while -computername SRV02 and -comp SRV02 use the full or abbreviated name. section 19.4

-Path <String[]>
    Specifies a path to one or more locations. Wildcards are
    permitted. The default location is the current directory (.).
    Required?                    false
    Position?                    0
    Default value                Current directory
    Accept pipeline input?       true (ByValue, ByPropertyName)
    Accept wildcard characters?  True

Inspect the help entry to see -Path at position 0, its default location, and its accepted input. section 4.6.3

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Explains syntax markers, position numbers, ordering rules, mixed usage, and readability tradeoffs without covering parameter binding internals.
Examples
Demonstrates Get-Item, Get-ChildItem, Get-Help, and Get-DiskInventory.ps1 with positional and named forms.
What it assumes
Assumes basic PowerShell command use and teaches how to discover parameter positions through syntax summaries and full help.
What you can do after
Apply it via PowerShell by reading help syntax, supplying positional values correctly, and choosing named parameters for durable scripts.
How it connects
Connects positional parameters with PowerShell commands, named parameters, command syntax, help systems, and scripts.

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 in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.