PowerShell Arrays

Quick reference for creating, manipulating, searching, and optimizing array structures (fixed, ArrayList, and generic List[T]) in PowerShell.

#Array Basics

#Create & Inspect

$myArray = @('test1','test2','test3')
$myArray.GetType().Name      # => Object[]
$myArray.Count               # => 3
$myArray                     # => test1, test2, test3

#Collect & Single‑Item

@(Get-Process | Select-Object -First 2).Count  # => 2
,42 | ForEach-Object GetType | `
  Select-Object -Expand Name                   # => Int32
@()                                            # => empty array

#Strongly Typed Arrays

[int[]]$nums = 1,2,3
$nums += 4                     # => 1,2,3,4 (new array)
[string[]]$s = @()             # => typed empty array

#Add/Remove Items

#Append (arrays are fixed-size)

$myArray = $myArray + 'test4'  # => test1..test4
$myArray += 'test5'            # => test1..test5
$myArray                       # => test1, test2, test3, test4, test5

#Remove by Value (filter to new array)

'remove test2:' | Write-Output
$myArray = $myArray -ne 'test2'   # keep items != 'test2'
$myArray                           # => test1, test3, test4, test5

#Keep / Drop Multiple

$myArray = $myArray -notin @('test1','test5') # drop both
$myArray                                      # => test3, test4
$only3 = $myArray -in @('test3')              # => test3

#Indexing & Slicing

#Index & Range

$myArray = 'a','b','c','d'
$myArray[0]                  # => a
$myArray[-1]                 # => d
$myArray[1..2]               # => b, c
$myArray[2,0]                # => c, a

#Insert by Rebuild

$idx = 1
$myArray = $myArray[0..($idx-1)] + 'X' + `
           $myArray[$idx..($myArray.Count-1)]
$myArray                      # => a, X, b, c, d

#Jagged vs Multidimensional

$jag = @(@(1,2), @(3,4)); $jag[1][0]  # => 3
$grid = New-Object 'object[,]' 2,3
$grid[0,1] = 'A'; $grid[0,1]         # => A

#Pipeline & Script Methods

$arr = 'test1','test2','prod1'
$arr | Where-Object { $_ -like 'test*' }      # => test1, test2
$arr.Where({ $_ -ne 'test2' })                # => test1, prod1
$arr.ForEach({ $_.ToUpper() })                # => TEST1, TEST2, PROD1

#Membership & Index

$arr -contains 'prod1'        # => True
'prod1' -in $arr              # => True
$arr.IndexOf('test2')         # => 1 (or -1 if not found)

#Unique & Grouping

('a','b','a') | Select-Object -Unique        # => a, b
('a','b','a') | Group-Object | `
  Sort-Object Count -Desc | `
  ForEach-Object { "$($_.Name):$($_.Count)" } # => a:2, b:1

#Join/Split/Sort/Reverse

#Join & Split

$csv = ('x','y','z') -join ','    # => x,y,z
'one,two,three' -split ','        # => one, two, three

#Sort & Reverse

$nums = 5,3,9,1
$nums | Sort-Object               # => 1, 3, 5, 9
[Array]::Reverse($nums); $nums    # => 9, 5, 3, 1

#Map & Reduce

$nums | ForEach-Object { $_ * 2 }          # => 10,6,18,2
($nums | Measure-Object -Sum).Sum          # => 18

#ArrayList (Resizable)

#ArrayList Create & Inspect

$myArrayList = [System.Collections.ArrayList]@()
$myList = New-Object System.Collections.ArrayList  # recommended
$myList.GetType().Name            # => ArrayList
$myList.IsFixedSize               # => False

#Add / AddRange / Show

$myList.Add('itemA')              # => 0 (index)
[void]$myList.Add('itemB')        # => (no output)
$myList.AddRange(@('itemC','itemC')) # adds multiple items
$myList                           # => itemA, itemB, itemC, itemC

#Remove / RemoveRange / Insert

''; 'remove itemC:' | Write-Output
$myList.Remove('itemC')           # removes first 'itemC'
$myList                           # => itemA, itemB, itemC

''; 'remove range:' | Write-Output
$myList.RemoveRange(0,1)          # remove 1 from index 0
$myList                           # => itemB, itemC

$myList.Insert(0,'HEAD')          # insert at index
$myList                           # => HEAD, itemB, itemC

#Other Ops

$myList.Contains('itemB')         # => True
$myList.IndexOf('itemB')          # => 1
$myList.Clear(); $myList.Count    # => 0

#Generic List[T] (Typed)

#Why & How

# Faster than ArrayList; type-safe
$list = [System.Collections.Generic.List[string]]::new()
$list.Add('one'); $list.AddRange(@('two','three'))
$list[1]                              # => two

#Convert Array → List

$arr = 'a','b','c'
$list = [System.Collections.Generic.List[string]]$arr
$list.Add('d'); $list                 # => a, b, c, d

#Remove & Capacity

$list.Remove('two')                   # => True
$list.RemoveAt(0); $list              # => c, d
"$($list.Count)/$($list.Capacity)"    # => e.g. 2/4

#Tips & Pitfalls

#Tips

  • + / += on arrays creates a new array (copy cost).
  • Use ArrayList / List[T] for frequent inserts/removals.
  • Use filtering (Where-Object / .Where()) to “remove” items.
  • ,x forces a single‑item array; @() collects pipeline output.
  • Typed arrays ([int[]]) enforce element types on assignment.
  • -contains / -in are membership (scalar) operators.
  • Prefer List[T] over ArrayList when you want type safety.