Powershell

How to Pass Multiple Parameters Into a Function in PowerShell

Functions in PowerShell contain the statements or instructions specified by the user. They are the chunks of code utilized to perform specific tasks, such as copying files or outputting text into a file. Moreover, functions can be reused anywhere in the code by calling the relevant name. In functions, single or multiple parameters are used to provide the input to the given function.

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:

param (param1, param2, param3...)

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:

test1 {

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:

function Test2 {

Write-Host Name: $args[0]

Write-Host Gender: $args[1]

Write-Host Age: $args[2]

}

Test2 "Willy" "Boy" "23"

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.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.