Arrays

Arrays are defined as comma separated

Arrays

Basically, arrays are defined as comma separated.

PS> $array = "first", "second"
PS> $array
first
second
PS> $array = 1..4 ## For continuous values
PS> $array
1
2
3
4

Accessing elements

PS> $array[1]
2
PS> $array[1,3]
2
4
PS> $array[1..3 + 0]
2
3
4
1
PS> $array[-1]
4
PS> $array[3..0]                
4
3
2
1
PS> $array[1..-1]
2
1
4

Empty arrays and arrays of arrays

PS> $jag = @() # Empty arrays
PS> $jag = @((1,2),(3,4)) # Array of arrays
PS> $jag[0][1]
2

Variable assignment using arrays

You can use unpacked assignments in the same way as in Python.

PS> $a,$b,$c,$d = $array
PS> $a;$b;$c;$d
1
2
3
4
PS> $first,$second = $jag
PS> $first
1
2
PS> $second
3
4

Enumerating array elements (PowerShell V3 or later)

If the variable is an array, you can write “array.property” to enumerate the properties of all the elements.

PS C:\Users\user\Desktop\test> $item = @(Get-ChildItem) # An example of how you can force it to be an array by enclosing it in @(). This can be done without it.
PS C:\Users\user\Desktop\test> $item.FullName
C:\Users\user\Desktop\test\test
C:\Users\user\Desktop\test\test2
C:\Users\user\Desktop\test\test3

Perform processing on all array elements.

There are several ways to do this.

  • ForEach method
  • ForEach-Object cmdlet
  • Foreach statement

ForEach method

PS> $array = 1..4
PS> $sum = 0
PS> $array.ForEach({$sum += $_})
PS> $sum                        
10

ForEach-Object cmdlet

PS> $sum = 0
PS> $array | ForEach-Object { $sum += $_ }
PS> $sum
10

Foreach statement

PS> $sum = 0
PS> foreach($element in $array){ $sum += $element }
PS> $sum
10

Merge arrays

You can join arrays with +.

PS> $elements = "Element1", "Element2", "Element1", "Element19"
PS> $result = $array + $elements # Merge arrays
PS> $result
1
2
3
4
Element1
Element2
Element1
Element19

Repeating arrays

You can repeat an array by multiplication.

PS> $array * 2
1
2
3
4
1
2
3
4

Finding elements in an array

To find an element, use the comparison operator.

  • -eq
  • -like
  • -match
PS> $elements -eq "Element1"
Element1
Element1
PS> $elements -like "*1*" # -like is a wildcard comparison
Element1
Element1
Element19
PS> $elements -match "Element.." # -match is a regular expression comparison
Element19

Large-small comparisons need to be careful about sort order.

PS> $elements -lt "Element2"
Element1
Element1
Element19   # Unexpected elements!
PS> $elements | sort
Element1
Element1
Element19
Element2

Where method

Where method can be used to filter the elements of an array.

# Syntax
Where(scriptblock expression[, WhereOperatorSelectionMode mode
                            [, int numberToReturn]])
PS> $array.Where{$_ % 2}        
1
3
PS> $array.Where{$_ % 2 -eq 0}  
2
4

# Same result
PS> $array.Where({$_ % 2})
1
3
PS> $array.Where({$_ % 2 -eq 0})
2
4

Where method allows you to specify the mode.

Mode Description
Default (0) returns all items
First (1) returns the first item
Last (2) returns the last item
SkipUntil (3) skip items until the condition is met and return the remaining items
Until (4) return all items until condition matches
Split (5) Returns the item that matches the element as the first element.
Returns all other items as the second element
PS> $array.Where({$_ -gt 2}, 'Default' , 1)
3
PS> $array.Where({$_ -gt 2}, 'First')
3
PS> $array.Where({$_ -gt 2}, 'Last') 
4
PS> $array.Where({$_ -gt 2}, 'SkipUntil')
3
4
PS> $array.Where({$_ -gt 2}, 'Until')
1
2
PS> $array.Where({$_ -gt 2}, 'Split')
3
4
1
2

Remove an element from an array.

Filter arrays, using the -ne, -notlike, and -notmatch comparison operators.

PS> $elements -ne "Element1"
Element2
Element19
PS> $elements -notlike "*1*"
Element2
PS> $elements -notmatch "Element.."
Element1
Element2
Element1

To remove an element from an array, reassign it to the original array.

PS> $elements = $elements -ne "Element1"
PS> $elements
Element2
Element19

The $null assignment does not remove any element from the array.
The reason is that arrays are of fixed length in PowerShell.

PS> $elements = "Element1", "Element2", "Element1", "Element19"
PS> $elements.Length
4
PS> $elements[0] = $null # $null assignment
PS> $elements # It looks like the elements of the array have been removed
Element2
Element1
Element19
PS> $elements.Length
4 # Unexpected! The number of elements in the array has not changed from before the deletion.

Check if the element is contained in an array

Use the following operators.

  • -contains
  • -notcontains
  • -in
  • -notin
PS> $array -contains 4
True
PS> $array -notcontains 4
False
PS> 4 -in $array # Available since PowerShell V3.
True
PS> 4 -notin $array # Available since PowerShell V3.
False

Be careful about the order of arrays and values.

Array -[not]contains value
Value -[not]in array

String array operators

Join elements

Use the -join operator.

PS> $text = "Power","Shell"

# If there is a value, join by that value.
PS> $text -join ","
Power,Shell

# If there is no value, simply join.
PS> -join $text
PowerShell

Split

Use the -split operator.

PS> $joined = 'Power,Shell'

# If there is a value, split by that value.
PS> $joined -split ","
Power
Shell

# If there is no value, it will automatically split.
PS> $text = "Power`r`n `tShell"
PS> $text
Power
Shell
PS> $text.count 
1
PS> -split $text
Power
Shell
PS> (-split $text).count
2

Splitting (Split method and Split operator)

There are multiple ways to split a string.

  • Split method (available from PowerShell V1)
  • Split operator (available from PowerShell V2)
PS> ${power shell} = "Power Shell"

# Split method
PS> ${power shell}.Split()
Power
Shell

# Split operator
PS> -split ${power shell}
Power
Shell

YouTube

Click here for a video explanation.

About Arrays | Microsoft Docs

About join | Microsoft Docs

About Split | Microsoft Docs