Table of Contents
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.
1 2 3 4 5 |
foreach ($arg in $args) { Write-Host "Argument: $arg" } |
1 2 3 |
.\MyScript.ps1 "Param1" "Param2" "Param3" |
1 2 3 4 5 |
Argument: Param1 Argument: Param2 Argument: Param3 |
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.
1 2 3 4 |
Write-Host "First argument is "$args[0] Write-Host "Second argument is "$args[1] |
1 2 3 |
.\MyScript.ps1 "Hello" "World" |
1 2 3 4 |
First argument is Hello Second argument is World |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
param( [string]$userName, [int]$userId, [string]$ProductName, [int]$ProductPrice ) write-output "User name is $userName" write-output "User id is $userId" write-output "Product name is $ProductName" write-output "Product Price is $ProductPrice" |
1 2 3 |
.\MyScript.ps1 -userName "Ayesha" -userId 689 -productName mouse -productPrice 200 |
1 2 3 4 5 6 |
User name is Ayesha User id is 689 Product name is mouse Product Price is 200 |
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.
1 2 3 4 5 6 7 8 |
param ( [string]$Param1, [string]$Param2 ) Write-Host "Param1: $Param1" Write-Host "Param2: $Param2" |
1 2 3 4 5 6 7 |
$params = @{ Param1 = "value1" Param2 = "value2" } & .\Myscript.ps1 @params |
1 2 3 4 |
Param1: value1 Param2: value2 |
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.
Further reading:
Using ValueFromRemainingArguments
Parameter
Use the ValueFromRemainingArguments
parameter to pass multiple parameters to PowerShell script.
1 2 3 4 5 6 7 8 9 10 11 |
param( [Parameter(ValueFromRemainingArguments=$true)] [string[]]$Param1 ) $i = 1 foreach ($value in $Param1) { Write-Host "Param${i}: {value}" $i++ } |
1 2 3 |
.\myscript.ps1 value1 value2 value3 |
1 2 3 4 5 |
Param1: value1 Param1: value2 Param1: value3 |
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 totrue
. 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:
1 2 3 4 5 6 7 8 9 10 |
param( [Parameter(Position=0)][String]$Vlan, [Parameter(ValueFromRemainingArguments=$true)][String[]]$Hosts ) Write-Host "Param: ${Vlan}" foreach ($p in $Hosts) { Write-Host "Param: $p" } |
1 2 3 |
.\MyScript.ps1 VLAN1 Host1 Host2 Host3 |
1 2 3 4 5 6 |
Param: VLAN1 Param: Host1 Param: Host2 Param: Host3 |
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.