Function
Parameter
Parameters are wrapped inside a function block with param(...)
ps1
function Foo {
param (
$Foo
)
}
1
2
3
4
5
2
3
4
5
NOTE
Default type of a parameter is System.Object
.
Positional Parameter
Positional parameters allows passing values with explicit names.
ps1
function Foo {
param (
[string] $Foo,
[string] $Bar
)
Write-Output "$Foo $Bar"
}
Foo -Foo foo -Bar bar
Foo foo bar # it's the same
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Default Parameter
ps1
function Foo {
param (
[string]$foo = "foo"
)
}
1
2
3
4
5
2
3
4
5
Flags
Defining flags that represents a toggle needs a special type called switch
. switch
has the same nature of bool
, but bool
parameter requires explicit assignment when the function being called. While switch
will remain $false
when unspecified.
ps1
function Foo {
param (
[switch]$Foo
[bool]$Bar
)
}
# this is why we should use `switch` instead.
Foo -Foo -Bar $true
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
Required Parameter
All parameters are optional by default. Use [Parameter(Mandatory=$true)]
to mark it as required.
ps1
param (
[Parameter(Mandatory=$true)]
[string]$RequiredName
)
1
2
3
4
2
3
4
Parameter Alias
Parameters can have aliases. It's not needed for most of time though since pwsh can distinguish option by leading string.
ps1
function Person {
param (
[Alias("n")]
[string]$Name,
[Alias("a")]
[int]$Age
)
Write-Host "Name: $Name, Age: $Age"
}
Person -n "Alice" -a 30
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
Lifetime
- Function should be define before it was called.