$ary = 'foo', 'bar', 'baz'
write-output "First element is: $($ary[0])"
write-output "Second element is: $($ary[1])"
write-output "Last element is: $($ary[-1])"
write-output "The array has $($ary.count) elements"
#
# First element is: foo
# Second element is: bar
# Last element is: baz
# The array has 3 elements
# Iterating over the array using the foreach-object cmdlet …
$ary | foreach-object { $_.ToUpper() }
# … or the foreach statement:
foreach ($elem in $ary) {
$elem.ToUpper()
}
By default, the elements are printed one below an another when printing an array. In order to print all elements on one line, the -join operator can be used:
$days = 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
write-output $days
#
# Sun
# Mon
# Tue
# Wed
# Thu
# Fri
# Sat
write-output($days -join ' ')
#
# Sun Mon Tue Wed Thu Fri Sat
Creating arrays with the comma operator vs. the plus operator
An array can be created with the comma operator ($ary = $foo, $bar, $baz). If one of the elements from which the array is created (for example $bar) is itself an array, the element is not unpacked (or exploded) so that the resulting array in this example always has three elements.
Sometimes, however, it is desired to unpack the elements of an array when a new array is created. This is possible by using the plus operator rather than the comma operator.
The following example tries to demonstrate exactly this:
$abc = 'a', 'b', 'c'
$abc.length
#
# 3
# ----- Create an array with the comma operator
#
# $abc is turned into one element so that
# the resulting array consists of 4 elements:
$ary = $abc, 'd', 'e', 'f'
$ary.length
#
# 4
$ary | foreach-object { "$_" }
#
# a b c
# d
# e
# f
# ----- Create an array with the plus operator
#
# The elements in $abc are exploded/expanded
# so that the resulting array consists
# of 6 elements:
$ary = $abc + 'd', 'e', 'f'
$ary.length
#
# 6
$ary | foreach-object { "$_" }
#
# a
# b
# c
# d
# e
# f
An array with one element can alternatively also be created with a leading comma. In this case, the comma is a unaryoperator that explicitly creates a one-element array:
PowerShell (or .NET?) distinguishes between weakly typed and strongly typed arrays:
$weakly___typed = 'ONE', 'TWO', 'THREE'
[string[]] $strongly_typed = 'one', 'two', 'three'
write-output "An weakly typed array is $($weakly___typed.GetType().FullName) and derives from $($weakly___typed.GetType().BaseType.FullName)"
write-output "An strongly typed array is $($strongly_typed.GetType().FullName) and derives from $($strongly_typed.GetType().BaseType.FullName)"
#
# An weakly typed array is System.Object[] and derives from System.Array
# An strongly typed array is System.String[] and derives from System.Array
The following function returns an array with two elements with differing types. When invoked, the returned elements get assigned to two corresponding variables:
Quite similarly (but less powerfully) to Python's iterable unpacking, an array with n elements can be assigned to a an array with m (m <= n) elements so that the (n-m+1) elements of the array being assigned become the elements in the right-most element of the array being assigned to:
#
# Define an array with five elements…
#
$numbers = 'one', 'two', 'three', 'four', 'five'
#
# … and assign it to an array with three elements:
#
$eins, $zwei, $rest = $numbers
#
# $rest will have remaining elements of $numbers:
#
"eins = $eins"
"zwei = $zwei"
"rest = $rest"
Calling functions, passing arguments separated with commas
PowerShell separates arguments by whitespace, not by comma, When calling a function or cmdLet. Thus, the individual arguments (or parameters) must not be separated with commas because the commas construct an array.
The following example passes one argument, namely an array of three elements, to the function/cmdlet:
some-func 'foo', 'bar', 'baz'
Note: this is not the case when calling a .NET method. The following invocation passes two parameters, the strings 'PATH' and 'user', although these are separated by a comma: