PowerShell PowerShell Functions Basic Parameters


A function can be defined with parameters using the param block:

function Write-Greeting {
    "Hello $name, you are $age years old."

Or using the simple function syntax:

function Write-Greeting ($name, $age) {
    "Hello $name, you are $age years old."

Note: Casting parameters is not required in either type of parameter definition.

Simple function syntax (SFS) has very limited capabilities in comparison to the param block.
Though you can define parameters to be exposed within the function, you cannot specify Parameter Attributes, utilize Parameter Validation, include [CmdletBinding()], with SFS (and this is a non-exhaustive list).

Functions can be invoked with ordered or named parameters.

The order of the parameters on the invocation is matched to the order of the declaration in the function header (by default), or can be specified using the Position Parameter Attribute (as shown in the advanced function example, above).

$greeting = Write-Greeting "Jim" 82

Alternatively, this function can be invoked with named parameters

$greeting = Write-Greeting -name "Bob" -age 82