This post has provided a complete guide to pass multiple parameters into the PowerShell function.
How to Pass Multiple Parameters into a Function in Powershell?
These are the methods that can be utilized to pass multiple parameters into a function:
Method 1: Pass Multiple Parameters into a Function in Powershell Using Param Function
In PowerShell, the “param” statement is used inside the function to pass the parameters. To utilize it in the script, follow the given syntax:
Here, “param1” and “param2”… are the parameters that are passed to the param() function.
Example
To know more about the usage of the param() function, add the following code to your script:
param($Name, $Gender, $Age)
"$Name is a $Gender"
"$Name is $Age years old"
}
Test1 "David" "Boy" "26"
According to the given script:
- First, define a function named “test1”.
- After that, invoke the “param()” function and passed three parameters to it.
- Then, add the relevant statement inside the function body.
- Finally, write the function name outside the function and pass the values to it:
Output
As you can see from the output, parameters have been passed and successfully accessed.
Method 2: Pass Multiple Parameters into a Function in Powershell Using args[ ] Array
Another method that can be used to pass the parameters to the function is using the “args[ ]” array method. This can be utilized for passing the parameter to a specific argument.
Example
Here is the demonstration of passing the arguments to a function using the “args[ ]” array method:
In the given code:
- First, we created a function and assigned the function name.
- Inside the function, we created multiple arguments using the “args[ ]” array.
- Finally, we called the function name outside the function and passed the multiple parameters:
Output
That was all about passing multiple parameters into a function in PowerShell.
Conclusion
Multiple parameters in PowerShell are passed into the function using several methods, but we have elaborated on two of them, which are the “param()” function and the “args[ ]” array. This article has explained the methods to pass multiple parameters into a PowerShell function.