Functions
Functions in PowerShell are reusable blocks of code that can accept parameters, return results, and even process piped input. They are essential for organizing and modularizing scripts, making them easier to read, maintain, and reuse.
1. What is a Function?
A function is a named block of code stored in the function:\
drive. Functions can:
Accept parameters.
Return results.
Process piped input.
Be called from scripts or the interactive shell.
Listing Functions
To see all available functions in your session:
Get-ChildItem Function:\

Viewing Function Code
To view the code of a specific function:
Get-Content Function:\mkdir

Or:
Get-Item Function:\mkdir | Select-Object -ExpandProperty ScriptBlock

2. Creating a Function
Basic Function
A function is defined using the function
keyword, followed by the function name and a script block ({}
).
Example:
function Say-Hello {
"Hello, World!"
}

Function with Parameters
Functions can accept parameters, which are variables passed into the function when it is called.
Example:
function Greet-User {
param (
[string]$Name
)
"Hello, $Name!"
}
Greet-User -Name "Alice"

Function with Multiple Parameters
You can define multiple parameters and even assign default values.
Example:
function Add-Numbers {
param (
[int]$Number1,
[int]$Number2 = 10
)
$Number1 + $Number2
}
Add-Numbers -Number1 5
3. Advanced Function Features
Switch Parameters
Switch parameters are boolean parameters that don’t require a value. They are either present ($true
) or absent ($false
).
Example:
function Test-Connection {
param (
[string]$ComputerName,
[switch]$Ping
)
if ($Ping) {
Test-Connection -ComputerName $ComputerName -Count 1
} else {
Test-NetConnection -ComputerName $ComputerName
}
}
Test-Connection -ComputerName "server01" -Ping
Default Parameter Values
You can assign default values to parameters, which are used if no value is provided.
Example:
function Get-DiskUsage {
param (
[string]$Drive = "C:"
)
Get-Volume -DriveLetter $Drive
}
Get-DiskUsage
Parameter Types
You can enforce specific data types for parameters.
Example:
function Multiply-Numbers {
param (
[int]$Number1,
[int]$Number2
)
$Number1 * $Number2
}
Multiply-Numbers -Number1 5 -Number2 3
4. Processing Piped Input
Functions can process objects piped into them using the $input
variable or the Process
block.
Using $input
The $input
variable contains all piped objects.
Example:
function Process-Input {
foreach ($item in $input) {
"Processing: $item"
}
}
1, 2, 3 | Process-Input

Using the Process
Block
The Process
block processes each piped object individually.
Example:
function Process-Input {
process {
"Processing: $_"
}
}
1, 2, 3 | Process-Input
5. Returning Results
Functions return the output of their last executed command. You can use the return
keyword to explicitly return a value and exit the function.
Example:
function Add-Numbers {
param (
[int]$Number1,
[int]$Number2
)
return $Number1 + $Number2
}
$result = Add-Numbers -Number1 5 -Number2 3
6. Error Handling
You can control how errors are handled using the -ErrorAction
parameter.
Example:
function Test-Connection {
param (
[string]$ComputerName
)
Test-Connection -ComputerName $ComputerName -ErrorAction SilentlyContinue
}
Test-Connection -ComputerName "nonexistent"
Last updated