As a PowerShell developer, you should know how to use parameters in PowerShell functions. In this tutorial, I will show you how to create PowerShell functions with parameters, including both single and multiple parameters.
PowerShell function with Parameter
Let’s start with a simple example of a PowerShell function that accepts a single parameter. This can be useful when you need to pass a single piece of data to your PowerShell function.
Syntax
The basic syntax for a PowerShell function with a single parameter is like this:
function Function-Name {
param (
[ParameterType]$ParameterName
)
# Function logic here
}Example
Let’s create a function in PowerShell that takes a name as a parameter and prints a greeting message.
function Greet-User {
param (
[string]$Name
)
Write-Output "Hello, $Name!"
}
# Calling the function
Greet-User -Name "Alice"Here is how it works:
- Function Definition: The function
Greet-Useris defined using thefunctionkeyword. - Parameter Definition: The
paramblock is used to define parameters. Here, we define a single parameter$Nameof type[string]. - Function Logic: The
Write-Outputcmdlet is used to print a greeting message that includes the value of the$Nameparameter. - Function Call: The function is called with the
-Nameparameter, passing the value"Alice".
I have executed the above PowerShell script using VS code and you can see the output in the screenshot below:

Read How to Handle Errors with Try-Catch in PowerShell?
PowerShell Function with Multiple Parameters
Sometimes, you need to pass multiple parameters to a PowerShell function. PowerShell makes it easy to define functions with multiple parameters.
Syntax
The syntax for a PowerShell function with multiple parameters is similar to that of a single parameter function but with multiple parameters defined within the param block.
function Function-Name {
param (
[ParameterType1]$ParameterName1,
[ParameterType2]$ParameterName2
)
# Function logic here
}Example
Here is a complete example to create a function that takes two parameters: a name and an age, and prints a message.
function Introduce-User {
param (
[string]$Name,
[int]$Age
)
Write-Output "Hello, my name is $Name and I am $Age years old."
}
# Calling the function
Introduce-User -Name "Bob" -Age 30Here is how it works:
- Function Definition: The function
Introduce-Useris defined. - Parameter Definition: The
paramblock now includes two parameters:$Nameof type[string]and$Ageof type[int]. - Function Logic: The
Write-Outputcmdlet prints a message that includes both the$Nameand$Ageparameters. - Function Call: The function is called with the
-Nameand-Ageparameters, passing the values"Bob"and30, respectively.
You can see the output in the screenshot below after I executed the PowerShell script above.

PowerShell Function with Named Parameters
In PowerShell, Named parameters allow you to specify parameters by name when calling the function.
Here is a complete example and full PowerShell script.
function Calculate-Sum {
param (
[int]$Number1,
[int]$Number2
)
$Sum = $Number1 + $Number2
Write-Output "The sum of $Number1 and $Number2 is $Sum."
}
# Calling the function with named parameters
Calculate-Sum -Number1 5 -Number2 10PowerShell Function with Positional Parameters
Positional parameters allow you to omit the parameter names when calling the function in PowerShell. The order of the arguments determines which parameter they correspond to.
function Calculate-Sum {
param (
[int]$Number1,
[int]$Number2
)
$Sum = $Number1 + $Number2
Write-Output "The sum of $Number1 and $Number2 is $Sum."
}
# Calling the function with positional parameters
Calculate-Sum 5 10PowerShell Function with Switch Parameters
Switch parameters are used for boolean flags in a PowerShell function. They do not require a value; their presence alone indicates true.
function Show-Details {
param (
[switch]$Verbose
)
if ($Verbose) {
Write-Output "Showing detailed information..."
} else {
Write-Output "Showing basic information..."
}
}
# Calling the function with the switch parameter
Show-Details -VerboseI hope you now know how to create functions with parameters in PowerShell using these examples.
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.