Arrays - http://ss64.com/ps/syntax-arrays.html
A PowerShell array holds a list of data items.
The data elements of a PowerShell array need not be of the same type, unless the data type is declared (strongly typed).
Creating Arrays
To create an Array just separate the elements with commas.
Create an array named $myArray containing elements with a mix of data types:
$myArray = 1,"Hello",3.5,"World"
or using explicit syntax:
$myArray = @(1,"Hello",3.5,"World")
To distribute the values back into individual variables:
$var1,$var2,$var3,$var4=$myArray
Create an array containing several numeric (int) values:
$myArray = 1,2,3,4,5,6,7
or using the range operator (..):
$myArray = (1..7)
or strongly typed:
[int[]] $myArray = 12,64,8,64,12
Create an empty array:
$myArray = @()
Create an array with a single element:
$myArray = @("Hello World")
Create a Multi-dimensional array:
$myMultiArray = @(
(1,2,3),
(40,50,60)
)
Add values to an Array.
This is done using the += operator
Append an extra element with a value of 25 to the $monthly_sales array:
$monthly_sales += 25
Retrieve items from an Array
To retrieve an element, specify its number, PowerShell numbers the array elements starting at 0.
Return all the elements in an array:
$myArray
Return the first element in an array:
$myArray[0]
Return the seventh element in an array:
$myArray[6]
Return the 5th element through to the 10th element in an array:
$myArray[4..9]
Return the last element in an array:
$myArray[-1]
Return the first element from the first row in a multi-dimensional array:
$myMultiArray[0][0]
You can also combine named elements with a range, separating them with a +
so $myArray[1,2+4..9] will return the elements at indices 1 and 2 and then 4 through 9 inclusive.
Return the length of an array (how many items are in the array):
$myArray.length
Loop through the elements in an array:
foreach ($element in $myArray) {$element}
When you create an array without specifying a datatype, PowerShell will create the array as an object array.
To determine the data type of an array:
$myArray.gettype()
PowerShell is not case-sensitive so $myArray is treated the same as $Myarray
If you pipe an array to Get-Member, it will display information about the objects in the array. If you use Get-Member -InputObject, that will display information about the array itself:
get-member -inputobject $myArray
Set values
To change values in an array after it has been created, use the assignment operator (=) to specify a new value.
$myArray[4]=64
Alternatively use the SetValue method:
$myArray.SetValue(64,4)
$myMultiArray[2][4]=128
Add one array to another.
This creates a new array containing all the values from the first two arrays:
$march=@(2, 3, 4, 5, 6)
$april=@(7, 8, 9, 10, 11, 12)
$all = $march + $april
Delete an Array (by deleting the array variable)
Remove-Item variable:monthly_sales
“Nor shall you by your presence make afraid, The kingfisher, who looks down dreamily, At his own shadow gorgeously array'd” - Sir Edmund William Gosse
Related:
get-help about_array