liveBook · Manning

Default value

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

A default parameter value is an input automatically supplied to a command parameter when the caller does not provide one.

A default value supplies a parameter automatically when someone omits that input. In PowerShell, a Param() declaration can give a script parameter a practical fallback such as localhost. section 19.4

Disk inventory with an optional computer name

Declare $computername = 'localhost', then run .\Get-DiskInventory.ps1 without a computer name or provide one positionally or by name. Adding $drivetype = 3 lets either parameter be omitted independently. section 19.4

PowerShell also stores session-wide parameter defaults in $PSDefaultParameterValues. Values or script blocks can apply defaults to selected commands, while script-local settings remain scoped to that script. section 26.6

PS C:\> .\Get-DiskInventory.ps1 SRV1 3
PS C:\> .\Get-DiskInventory.ps1 -ComputerName SRV1 -drive 3
PS C:\> .\Get-DiskInventory.ps1 SRV1
PS C:\> .\Get-DiskInventory.ps1 -drive 3

Compare positional, abbreviated, and omitted parameters in runnable script calls. section 19.4

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

How deep it goes
Explains declarations, omitted arguments, session defaults, script blocks, and scope, but stops short of deeper parameter validation.
Examples
Demonstrates disk inventory parameters, localhost and drive-type fallbacks, credential defaults, and credential-prompting script blocks.
What it assumes
Assumes familiarity with PowerShell variables and basic command invocation while introducing parameter declarations from a practical scripting task.
What you can do after
Apply default values in PowerShell scripts and configure command-specific or session-wide defaults through $PSDefaultParameterValues.
How it connects
Connects default values with PowerShell parameters, positional parameters, parameter name abbreviation, variables, and scope.

Also explained in

Compare how 20 books explain default value →

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.