Handle Arguments in a PowerShell Script

Learn ways to handle arguments sent to a PowerShell script

So far, I haven’t used Powershell often in my work. I do know its a powerful tool and one that has saved me a lot of headaches when I have used it. One thing I recently found to be quite helpful is the ability to leverage the arguments sent to a PowerShell script file.

Here is the sample script that will be referenced throughout this article:

# ./HandlingArguments.ps1
# Args: 0 - string1; 1 - string2;
$args | ForEach-Object {
$arg = $_;
Write-Host $arg.GetType()
Write-Host $arg
Write-Host $arg[0]
Write-Host $arg[1]
}

I will begin by briefly describing this script. Certainly, the first two lines are just comments providing the name of the script file and some generic information about the args.

Next, is the use of the $args automatic variable. This variable contains an array of the arguments send to the script file. ForEach-Object provides us the ability to iterative over a collection of objects. Each item is an object within the collection and is represented inside the ForEach-Object body (if using a script block) as $_. Of course, I cannot forget about the symbol between $args and ForEach-Object, the | or “pipe” character. This character is used to pipe the input objects ($args array items) to ForEach-Object.

Variations of Argument Passing

There are various ways of passing data to this script. In the following example, we’ll simply pass the string “hello”:

PS> .\HandlingArguments.ps1 "hello"
System.String
hello
h
e

Here, the “hello” string is passed to the script and $args is an array with the string value “hello” as an item.

$_ is the string value “hello”. $_.GetType() gives us the System.String type. $_[0] and $_[1] access characters of the string “hello” (“h” and “e” respectively).

The important part to remember is that $args is an array of items passed to the script. When we iterate over that array we get the item value. In this case, that item value was “hello”.

Next, let’s send “hello” and “world” as two separate arguments to the script file:

PS> .\HandlingArguments.ps1 "hello" "world"
System.String
hello
h
e
System.String
world
w
o

Here, we see that there are now two sets of four outputs. The first mirrors the output when we just sent “hello”. The second is similar to the first but with the string “world” instead. Remember, $args is an array of items passed to the script. In this case, it is an array with two items: “hello” and “world”.

Next, let’s see what happens when we pass an array of strings to the script:

PS> .\HandlingArguments.ps1 ("hello", "world")
System.Object[]
hello world
hello
world

Here, we can see that we are back to just a single set of four outputs. $args is an array of items passed to the script. Since we passed a single array, it is an array consisting of a single item which is an array. $_ is the array containing “hello” and “world” as its items. $_.GetType() gives us the System.Object[] type. $_[0] and $_[1] access items within the array (“hello” and “world” respectively).

This article briefly described some of the ways to leverage the flexibility of Powershell to provide different output without changing the script file by simply changing the arguments passed to it.