PowerShell Array
Array is a list of values. It can be created by:
$a = 3,4,5 $a
can be multiple lines:
$rr = "c:/Users/xah/cat.html", "c:/Users/xah/dog.html", "c:/Users/xah/rabbit.html" # $rr = 1,2,3 $rr.foreach({ echo $_ })
WARNING: Last item must not have comma. And best to add a SEMICOLON ; at the end. This is a frequent error if you write PowerShell script.
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 @(dir).length
length
use method
length
or its alias
count
$t = 0..9 $t.length # 10
get elements
get one element
negative index count from right.
$t = 0..9 $t[2] # returns 2
get a multiple elements
# get index 3 and 6 (0..9)[3,6] # result is 3,6
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
Iterate Over Array
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 to String
# array to string [string] (1..9) # "1 2 3 4 5 6 7 8 9"
Array Methods
# list 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()