How to Pass Parameters into PowerShell Function
Problem
If you are new to PowerShell, you may be wondering how to pass multiple parameters into a function. Just like in traditional programming language, this capability is useful since it allows you to reuse the code and minimizes the errors. This article will guide you to do that.
Solution
First, let’s take a look at the different ways you can define parameters in PowerShell. There are three ways to do this:
Positional Parameters
In this method, we pass the parameters according to the position, thus it will be order-dependent. The parameters must be separated by space and without parentheses which is in line with other shell language like bash
.
Function MyFunction($param1, $param2)
{
Write-Host "`$param1 value: $param1"
Write-Host "`$param2 value: $param2"
}
MyFunction "ABC" "DEF"
Output:
$param1 value: ABC
$param2 value: DEF
We cannot use comma as separator because comma is array operator.
$arr = "1","2","3"
Write-Host $arr
Output:
1 2 3
Actually, parentheses can still be used to separate the parameters. If you try below code, then you will get the same result as when you don’t use parentheses
Function MyFunction($param1, $param2)
{
Write-Host "`$param1 value: $param1"
Write-Host "`$param2 value: $param2"
}
MyFunction ("ABC") ("DEF")
Output:
$param1 value: ABC
$param2 value: DEF
However, you cannot put the arguments you want to pass inside parentheses and separated by comma. As mentioned before, the result won’t be as expected because comma is an array operator.
Function MyFunction($param1, $param2)
{
Write-Host "`$param1 value: $param1"
Write-Host "`$param2 value: $param2"
}
MyFunction ("ABC","DEF")
Output:
$param1 value: ABC DEF
$param2 value:
The above code will mean we pass an array to the first parameter of the function and we pass nothing to the second parameter, thus the value of second parameter will be empty or null.
Named Parameters
Named parameters allow us to pass the parameters independently without specific order. Named parameters start with a hyphen followed by the name of the parameter.
Function MyFunction($param1, $param2)
{
Write-Host "`$param1 value: $param1"
Write-Host "`$param2 value: $param2"
}
MyFunction -param2 "DEF" -param1 "ABC"
Output:
$param1 value: ABC
$param2 value: DEF
Splatting
Splatting is a way to pass a collection of parameters as a unit. It enable us to build the arguments dynamically.
Let’s say you want to test connection to 1000 websites that is stored in dataset or CSV file. You just need to convert dataset or export csv into array, then pass the array to Test-Connection
using Splatting syntax.
In this example, we use ‘HashTable Splatting’.
$argumentHash = @{
TargetName = 'www.google.com', 'www.youtube.com'
}
Test-Connection @argumentHash
Above script is equal to below script, but if you have a lot of websites to test, you must not want to use below scripts.
Test-Connection -TargetName www.google.com, www.youtube.com
Conclusion
Passing multiple parameters into a function in PowerShell is a great way to reuse code and minimize errors.
You can easily pass the parameters by using the position, the name or splatting. Using positional parameters means that you do not need to specify parameter names when calling the function, only their values in order from left to right.
Contrastingly, defining parameters by name means that you must include both the parameter name and its value when calling a function.
Meanwhile, splatting is the additional method if we want dynamically build the parameters and pass them as a whole.
Ultimately, it is up to you which method to use depending on your needs.