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:

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 = @(

Add values to an Array.

This is done using the += operator

Append an extra element with a value of 'India' to the $countries array:
$countries += 'India'

Adding items to a large array can be quite slow, a PowerShell array variable is immutable - meaning that in the background it creates a whole new array that includes the new value and then discards the old array.
A faster alternative to use a .Net ArrayList:

$countries = New-Object System.Collections.ArrayList
$countries.Add('India') > $null
$countries.Add('Montenegro') > $null

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:


Return the first element in an array:


Return the seventh element in an array:


Return the 5th element through to the 10th element in an array:


Return the last element in an array:


Return the first element from the first row in a multi-dimensional array:


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):


Loop through the elements in an array:

foreach ($element in $myArray) {$element}

In PowerShell 4.0+ arrays have the methods .Where() and .Foreach() a faster alternative to a traditional pipeline at the expense of a higher memory consumption:
@(Get-Service).Where({$_.Status -eq 'stopped'})
or omitting the parenthesis:
@(Get-Service).Where{$_.Status -eq 'stopped'}

Comparisons -eq - gt -lt etc

With an array, comparison operators will work as a filter returning all the values which match.

PS C:\> $a = 1,2,3,2
PS C:\> $a -eq 2

Care must be taken with comparing NULLs, because this is a filter you must place the $null on the left side of the comparison.

PS C:\> $a -eq $null # fails: returns $null
PS C:\> $null -eq $a # works: returns $false

Data Types

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:


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.


Alternatively use the SetValue method:



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 PowerShell Commands:

get-help about_array
Hash Tables & SPLAT operator
Variables - Create/read variables

Copyright © 1999-2018
Some rights reserved