PowerShell is complete scripting and programming language used to manage Windows systems. Like other programming languages, PowerShell supports functions and parameters. There are different use cases for PowerShell like creating a user, adding some data to the database even copying data. The terms parameter
and argument
is generally used interchangeably. So in this tutorial, we will refer to parameter or parameters.
Read Single Parameter/Argument
The most basic case for reading a single parameter or argument from the command-line interface in PowerShell. The $args[] array can be used to get provided parameter or argument. As an array it stores single or more items which are provided as parameter. The first item is stored with the index number zero.
$parameter1 = $args[0]
write-host $parameter1
We will call the PowerShell script named MyScript.ps1 and provide the “windowstect.com” as the parameter.
.\MyScript.ps1 "windowstect.com"

Read Multiple (Two or more) Parameters/Arguments
The array $args[] can be used to read multiple parameters. These parameters can be two or more. Every item in the array $arg[] stored a parameter according to the provided sequence. In the following example, we will read 3 parameters from the command-line interface to the MyScript.ps1.
$param1=$args[0]
$param2=$args[1]
$param3=$args[2]
write-host $param1
write-host $param2
write-host $param3
We provide “windowstect.com”, “linuxtect.com” and “wisetut.com” as 3 parameters.
.\MyScript.ps1 "windowstect.com" "linuxtect.com" "wisetut.com"

Read Bunch of (Alot of) Parameters/Arguments
In some cases we may need to read lot of parameters and store some of them specific variables etc. The for loop can be used in order to read a bunch of parameters. Every provided parameter is iterated with the for loop. The provided parameter count is stored as $args.count variable.
for ( $i=0; $i -lt $args.count; $i++){
write-host $args[$i]
}
In the following example we will call the MyScript.ps1 PowerShell script for multliple times with different count of parameters. Every time it will work and process multiple parameters without problem.
.\MyScript.ps1 "windowstect.com" "linuxtect.com"
.\MyScript.ps1 "windowstect.com" "linuxtect.com" "wisetut.com" "pythontect.com" "poftut.com"

Read Named Parameters/Argument
To make the PowerShell script more professional and easy to read the named parameters can be used to read parameters from the command line. The named parameters assign names to the different parameters by using the function param(). The sequence if important in order to set proper parameters to the specified named parameter.
param($name,$surname)
write-host "Name is $name"
write-host "Surname is $surname"
In the following example we will use the PowerShell named paramters which are assigned to the variables named $name
and $surname
. The function param()
is used to set them automatically.
.\MyScript.ps1 "İsmail" "Baydan"

Alternatively the named PowerShell parameters can be specify with their names defined in the function parameter()
. Only change is the $ sign is replaced with the – .
.\MyScript.ps1 -surname "Baydan" -name "İsmail"

Set Default Parameter/Argument Value
Another good practice while using parameters in PowerShell is setting a default value for parameters. The parameters can be assigned for default values and if the parameter is not specified while calling the PowerShell script the default value is used. In the following example, we will call the MyScript.ps1
and provide the parameter -name
and do not provide the -surname
.
param($name,$surname="Baydan")
write-host "Name is $name"
write-host "Surname is $surname"

I am trying to sort arguments with no luck. I would like sort them by length of the string.
code is as follows:
$param=$args[0]
$param2=$args[1]
$param3=$args[2]
write-host “result” $param $param2 $param3 | Sort-Object
./test.ps1 yellow red blue
result that i would like to have is
red blue yellow