PowerShell Array

By Xah Lee. Date: . Last updated: .

Array is a list of values. It can be created by:

$a = 3,4,5
$a

To create a array of single item, precede the item by a comma.

$a = ,4

create array by range operator

you can use range operator to create array.

$a = 3..7
# result 3 4 5 6 7

convert object collection to array

# @(objects) turn objects into array
$a = @("a", "b", "c")

# useful when some command return many objects, and you want to find count
@(Get-ChildItem).length

length of array

use method length or its alias count

$t = 0..9
$t.length
# 10

get elements

get a single element

$t = 0..9
$t[2] # returns 2

Get a slice

$t = 0..9
$t[3..6]
# result is 3,4,5,6

Get multiple slices

use + inside [] between slices, to get different elements.

$t = 0..99

# get array from index 1 to 10 and 30 to 40
$t[1..10+30..40]

set element

$r = 0..3
$r[0] = "b"
$r

or use method SetValues(val,index)

$r = 0..3
$r.SetValue("b",0)
$r

add element

Use operator += to add a element to end of array.

Note, the array data structure is optimized for not changing size. It has to recreate the array and is slow. The more elements in a array, the slower it is to change size.

$r = 0..3

# add a item "b" to the end
$r += "b"
$r

delete element

It's not easy to delete array element in PowerShell. Array data structure is not designed to change size. Instead of delete, just replace the value with $null.

join arrays

use + operator.

$a = 1,2
$b = 3,4,5
$a+$b

delete array

assign the whole to $null

$a = 1,2,3
$a = $null
$a

iteration over array

ForEach method is useful when you want to go thru all the values of array.

arrayX.ForEach(script_block)
iterate over array. In the script block, $_ is the value of current member.
$arr = 0..3
$arr.foreach({$_ + 1})
# print 1 to 4

ForEach method can be used to get or set property of each item in array.

arrayX.ForEach(property_name_str)
list each element's property.
@(dir).foreach("lastAccessTime")
arrayX.ForEach(property_name_str, new_val)
set each elemen's property to a new value.
arrayX.ForEach(method_name_str)
call a method on each element.
# list dir and show in upper case
@(dir -name).foreach("toUpper")

More loops

PowerShell: Loop, Iteration

reset all elements

array.Clear() Sets all element values to the default value of the array's element type.

$aa = 1..9
$aa.Clear()

filter array

(1,2,3,4,5).where({$_ -gt 3})
# return (4,5)

Array Methods

Get-Member -InputObject (1,2) -MemberType Method

Add
int IList.Add(System.Object value)
Address
System.Object&, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e Address(int )
Clear
void IList.Clear()
Clone
System.Object Clone(),
System.Object ICloneable.Clone()
CompareTo
int IStructuralComparable.CompareTo(System.Object other, System.Collections.IComparer comparer)
Contains
bool IList.Contains(System.Object value)
CopyTo
void CopyTo(array array, int index),
void CopyTo(array array, long index),
void ICollection.CopyTo(array array, int index)
Equals
bool Equals(System.Object obj),
bool IStructuralEquatable.Equals(System.Object other, System.Collections.IEqualityComparer comparer)
Get
System.Object Get(int )
GetEnumerator
System.Collections.IEnumerator GetEnumerator(),
System.Collections.IEnumerator IEnumerable.GetEnumerator()
GetHashCode
int GetHashCode(),
int IStructuralEquatable.GetHashCode(System.Collections.IEqualityComparer comparer)
GetLength
int GetLength(int dimension)
GetLongLength
long GetLongLength(int dimension)
GetLowerBound
int GetLowerBound(int dimension)
GetType
type GetType()
GetUpperBound
int GetUpperBound(int dimension)
GetValue
System.Object GetValue(Params int[] indices),
System.Object GetValue(int index),
System.Object GetValue(int index1, int index2),
System.Object GetValue(int index1, int index2, int index3),
System.Object GetValue(long index),
System.Object GetValue(long index1, long index2),
System.Object GetValue(long index1, long index2, long index3),
System.Object GetValue(Params long[] indices)
IndexOf
int IList.IndexOf(System.Object value)
Initialize
void Initialize()
Insert
void IList.Insert(int index, System.Object value)
Remove
void IList.Remove(System.Object value)
RemoveAt
void IList.RemoveAt(int index)
Set
void Set(int , System.Object )
SetValue
void SetValue(System.Object value, int index),
void SetValue(System.Object value, int index1, int index2),
void SetValue(System.Object value, int index1, int index2, int index3),
void SetValue(System.Object value, Params int[] indices),
void SetValue(System.Object value, long index),
void SetValue(System.Object value, long index1, long index2),
void SetValue(System.Object value, long index1, long index2, long index3),
void SetValue(System.Object value, Params long[] indices)
ToString
string ToString()

PowerShell

How-to

Advanced

Script Examples