Powershell

Passing Multiple Values to a Single PowerShell Script Parameter

PowerShell is a scripting tool for Windows used to create scripts and functions to perform specific operations. It uses the “param()” parameter method to allow the users to add input at the runtime of the function or script. Moreover, multiple values can be passed to a single PowerShell script parameter using the “param()” parameter method.

This write-up will observe several techniques to perform the stated operation.

How to Pass Multiple Values to a Single PowerShell Script Parameter?

Multiple values can be passed to a single PowerShell script parameter by passing the arguments. This can be accomplished by initially building a function and adding the “param()” method inside the function. Inside the “param()” method, specify the parameter you wish to pass the multiple values. Moreover, the “param()” method can also be used outside the function.

Example 1: Pass Multiple Values to a Single PowerShell Script Parameter Using “foreach()” Loop

This example will demonstrate a guide to pass multiple values to a PowerShell script parameter:

Param([string]$val)

foreach ($x in $args)

{

  Write-Host $x

}

Write-Host $val

According to this code:

  • First, create a “param()” method and pass the string variable “$val” inside it.
  • After that, add a “foreach()” loop, where “$x” is a reference, and it will find arguments in the “$args”.
  • Then, the “Write-Host $x” command is used within curly braces to display the output after taking values.
  • Outside the curly braces, the “Write-Host $val” code is defined to take values from the user:

Now, let’s pass the multiple values to a PowerShell script parameter using PowerShell console:

> C:\Doc\param.ps1 Welcome to LinuxHint

According to the above code, first, add the PowerShell script path and then add multiple values separated by space:

It can be observed that the multiple values have been passed to a PowerShell script parameter using the “foreach()” loop.

Example 2: Pass Multiple Values to a Single PowerShell Script Parameter Using function

Now, add the following code in the script:

function test {

  param($Name)

  "$Name is a doctor."

}

test James

test John

test David

According to the above code:

  • First, create a function, and add a “param()” block inside it.
  • Inside the “param()” block pass a parameter “$Name”.
  • Add the string value within inverted commas, and inside the string, add the parameter to be passed, which is “$Name”.
  • Now, outside the function, add the multiple function instances and a different value to each function instance:

Output

As can be seen that the multiple values to a single parameter script have been passed.

Conclusion

To pass multiple values to a single PowerShell script parameter, the “param()” method is used. It can be used to pass the parameters inside it. After that, add the function name outside the function and add the values you want to pass inside the script parameter. Moreover, values can also be passed to a single PowerShell script using the “foreach()” loop. This write-up will overview a comprehensive guide to pass multiple values to a PowerShell script parameter.

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.