Pass Multiple Parameters to PowerShell Script

Pass multiple parameters to Powershell script

All the code snippets are written in the MyScript.ps1 file; you can name this file to any name of your choice.

Using Automatic Variable $args

Use automatic variable $args to pass multiple parameters to PowerShell script.

The args variable is an array that contains the value of all the parameters passed to the script while calling the script. In the above example, 3 parameters "Param1" "Param2" "Param3" are passed while calling the MyScript.ps1 script file.

Space is used as a separator between the parameters.

It should also be noted that these parameters are unnamed and only contain values of parameters that are automatically stored in the args array. As we can observe, above in the MyScript.ps1 script file, the foreach loop is used to iterate through all the received arguments. Then, the Write-Host cmdlet displayed all the arguments on the console.

Let’s see another example below to get more understanding.

In the above case, the args array contained two elements "Hello" and "World", which were passed to the MyScript.ps1 file. These values can be accessed by referring to their position in the array. That is why these are also called positional parameters.

The first parameter value "Hello", is assigned to the first element of the array ($args[0]), and the second parameter value "World" is assigned to the second element ($args[2]) of the array.

Positional parameters are helpful when you know the exact number and order of parameters the script requires.

Using param() Block

Use the param() block to pass multiple parameters to PowerShell script.

In this method, the param() block in the MyScript.ps1 script defined the parameters with names and datatypes. The values of these parameters can be accessed anywhere in the script. The parameters are passed using the -ParameterName Value syntax when calling the script.

In the example provided, the script was called with four named parameters. The value "Ayesha" was assigned to the parameter "-userName", the value "689" was assigned to the parameter "-userId", the value "mouse" was assigned to the parameter "-productName", and the value "200" was assigned to the parameter "-productPrice". Then, the write-output cmdlet prints parameters with their values on the console.

The param() block should be at the start of the script.

Using Splatting

Use splatting to pass multiple parameters to PowerShell script.

In the given method, a hashtable is created, which contains the parameter names and their respective values. The call operator (&) is then used with the splatting syntax @params to pass the hashtable as named parameters to the PowerShell script.

Inside the MyScript.ps1 script, the named parameters can be accessed using the parameter names specified in the hashtable. This method is beneficial when you have numerous parameters or when the parameters have dynamic names/values that are unknown until runtime.

Note that the parameter names in the hashtable must match the names defined in the script’s param() block; otherwise,, the values will not be passed correctly.

Using ValueFromRemainingArguments Parameter

Use the ValueFromRemainingArguments parameter to pass multiple parameters to PowerShell script.

The ValueFromRemainingArguments parameters are defined in the script’s param() block and are assigned all remaining arguments as input. In this example, the $Param1 parameter is defined as a ValueFromRemainingArguments parameter, allowing it to accept all remaining arguments as input.

The value of the ValueFromRemainingArguments parameter is set to true. By default, its value is $false.

When calling the script, pass the array of parameters as separate values. For example, in the above case, the Value1, Value2, and Value3 are passed to the MyScript.ps1 script, which is then assigned to the $Param1 parameter as an array. Finally, the foreach loop is used to iterate through each element of the $Param1 array and display it on the console using the Write-Host cmdlet.

Let’s see another example:

The above PowerShell code demonstrates the usage of positional parameters and the ValueFromRemainingArguments parameter attribute in a script.

The script’s param() block defined two parameters – $Vlan and $Hosts. The first parameter, $Vlan, is a positional parameter with a position of 0. The second parameter, $Hosts, is a named parameter with the ValueFromRemainingArguments attribute set to $true. This means that $Hosts accepted an array of strings containing all the arguments that were not assigned to positional parameters.

While calling the script, four parameter values, VLAN1 Host1 Host2 Host3, are passed to the script. The first value, VLAN1, is assigned to the $Vlan variable; the rest of the arguments Host1, Host2, and Host3 are set to $Hosts as an array of strings in this PowerShell script.

Then, the Write-Host cmdlet is used to display the positional parameter $Vlan value. The remaining arguments Host1, Host2, and Host3 are also assigned to the $Hosts array.

That’s all about how to pass multiple paramerters to PowerShell Script.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *