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)
)
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-1
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
Add a new Element to an existing array
Append an extra element with a value of 25 to the $monthly_sales array:
$monthly_sales += 25
Delete an Array (by deleting the Array variable)
Remove-Item variable:monthly_sales
Hash Tables (also known as Associative arrays or Dictionaries)
This type of array allows you to store a collection of paired keys and values, rather like a simple database table.
Unlike normal arrays where you refer to each element via its numeric index, the keys of a hash table can be strings.
$array_name = @{key1 = item1; key2 = item2;...}
Hash table example:
$usa_states=@{ CA="California";
"NY" = "New York";
"IL" = "Illinois";
4 = 1,2,3}
Return just the New York element:
$usa_states.'NY'
Using @ as a SPLAT operator
New in PowerShell 2.0 is the ability to expand a hash table into a set of command line parameters.
First use @ to create a hashtable containing parameters to a cmdlet:
PS C:\> $params = @{path = "c:\demo"; Recurse= $true}
Then use @ to SPLAT the parameters:
PS C:\> dir @params
That will in effect run:
dir -Path c:\demo -Recurse:$true
When paired keys and values are used these will become cmdlet parameters and values:
PS C:\> $params = @{year = 1980; Month = 5; day = 31}
PS C:\> get-date @params
The @ character is also used to create here strings.
“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
Variables - Create/read variables