PowerShell Arrays
An array is a collection of values stored under a single variable name. Instead of creating separate variables for each item — $city1, $city2, $city3 — an array groups them all together as $cities. Arrays are the standard way to work with lists of data in PowerShell scripts.
What Is an Array?
Variable $cities holds 4 items:
+-------+-------+---------+--------+
| Index | 0 | 1 | 2 | 3 |
+-------+-------+---------+--------+--------+
| Value | Delhi | Mumbai | Chennai| Kolkata|
+-------+-------+---------+--------+--------+
Access: $cities[0] → Delhi
$cities[2] → Chennai
Arrays in PowerShell use zero-based indexing. The first element is at index 0, the second at index 1, and so on.
Creating Arrays
Method 1 – Using @() Syntax (Recommended)
$fruits = @("Apple", "Banana", "Mango", "Orange")
Write-Host $fruits
Method 2 – Comma-Separated Values
$numbers = 10, 20, 30, 40, 50
Write-Host $numbers
Method 3 – Range Operator
$range = 1..10 # Creates array: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Write-Host $range
Method 4 – Empty Array
$empty = @() # Empty array – items added later
Accessing Array Elements
$colors = @("Red", "Green", "Blue", "Yellow")
# Access by index (zero-based)
Write-Host $colors[0] # Red
Write-Host $colors[1] # Green
Write-Host $colors[3] # Yellow
# Access from the end using negative index
Write-Host $colors[-1] # Yellow (last item)
Write-Host $colors[-2] # Blue (second from last)
Array Properties
$scores = @(85, 92, 78, 95, 60)
# Number of items
Write-Host $scores.Count # 5
Write-Host $scores.Length # 5
# Data type of the array
Write-Host $scores.GetType().Name # Object[]
Modifying Array Elements
$days = @("Mon", "Tue", "Wed", "Thu", "Fri")
# Change the value at index 2
$days[2] = "Wednesday"
Write-Host $days[2] # Wednesday
Note: The default PowerShell array has a fixed size. Items cannot be added directly. Use the += operator to create a new expanded array.
Adding Items to an Array
$languages = @("PowerShell", "Python")
# Add an item using +=
$languages += "Bash"
$languages += "JavaScript"
Write-Host $languages
# Output: PowerShell Python Bash JavaScript
Write-Host $languages.Count # 4
Internally, += creates a brand-new array with all old items plus the new one. For large arrays with many additions, use ArrayList instead (covered below).
Removing Items From an Array
Standard arrays do not have a direct remove method. Filter out the unwanted item:
$fruits = @("Apple", "Banana", "Mango", "Orange")
# Remove "Banana" by filtering it out
$fruits = $fruits | Where-Object { $_ -ne "Banana" }
Write-Host $fruits
# Output: Apple Mango Orange
Iterating Over an Array
Using foreach Loop
$students = @("Anita", "Rohan", "Priya", "Dev")
foreach ($student in $students) {
Write-Host "Student: $student"
}
Output:
Student: Anita
Student: Rohan
Student: Priya
Student: Dev
Using for Loop with Index
$items = @("Laptop", "Mouse", "Keyboard")
for ($i = 0; $i -lt $items.Count; $i++) {
Write-Host "Item $($i+1): $($items[$i])"
}
Output:
Item 1: Laptop
Item 2: Mouse
Item 3: Keyboard
Sorting Arrays
$nums = @(5, 2, 8, 1, 9, 3)
# Sort ascending (default)
$sorted = $nums | Sort-Object
Write-Host $sorted # 1 2 3 5 8 9
# Sort descending
$sorted = $nums | Sort-Object -Descending
Write-Host $sorted # 9 8 5 3 2 1
# Sort strings alphabetically
$names = @("Zara", "Amit", "Priya", "Ben")
$names | Sort-Object # Amit Ben Priya Zara
Searching in Arrays
$tools = @("PowerShell", "Python", "Bash", "Ruby")
# Check if item exists
$tools -contains "Python" # True
$tools -contains "Java" # False
# Check if item is in the array (reverse syntax)
"Bash" -in $tools # True
# Find items matching a condition
$tools | Where-Object { $_ -like "P*" }
# Output: PowerShell Python
Array Slicing
$letters = @("A", "B", "C", "D", "E", "F")
# Get items from index 1 to 3
$slice = $letters[1..3]
Write-Host $slice # B C D
# Get first 3 items
$letters[0..2] # A B C
# Get last 2 items
$letters[-2..-1] # E F
Multi-Dimensional Arrays
PowerShell supports arrays of arrays (jagged arrays) for table-like data structures.
# Create a 2D-style array (array of arrays)
$matrix = @(
@(1, 2, 3),
@(4, 5, 6),
@(7, 8, 9)
)
# Access element at row 1, column 2
Write-Host $matrix[1][2] # Output: 6
# Loop through all rows and columns
foreach ($row in $matrix) {
Write-Host ($row -join " | ")
}
Output:
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
ArrayList – Dynamic Arrays
The .NET ArrayList is more efficient than a standard array when items are added or removed frequently. Unlike standard arrays, ArrayList resizes automatically without creating a new object.
# Create an ArrayList
$list = [System.Collections.ArrayList]::new()
# Add items
$list.Add("Windows") | Out-Null
$list.Add("Linux") | Out-Null
$list.Add("macOS") | Out-Null
Write-Host $list # Windows Linux macOS
# Remove an item by value
$list.Remove("Linux")
Write-Host $list # Windows macOS
# Remove by index
$list.RemoveAt(0)
Write-Host $list # macOS
# Count
Write-Host $list.Count # 1
The | Out-Null part suppresses the index number that Add() returns — it keeps the output clean.
Arrays vs ArrayList – Quick Comparison
| Feature | Standard Array | ArrayList |
|---|---|---|
| Fixed size | Yes (default) | No – dynamic |
| Add items | += (creates new array) | .Add() (fast) |
| Remove items | Filter with Where-Object | .Remove() / .RemoveAt() |
| Performance | Slow for frequent additions | Faster for large datasets |
| Best for | Small, fixed lists | Large, growing collections |
Array of Objects
# Array holding objects (common in real scripts)
$servers = @(
[PSCustomObject]@{ Name = "Web01"; Status = "Online" },
[PSCustomObject]@{ Name = "DB01"; Status = "Offline" },
[PSCustomObject]@{ Name = "Cache01"; Status = "Online" }
)
# Access a property
$servers[0].Name # Web01
$servers[1].Status # Offline
# Filter: show only online servers
$servers | Where-Object { $_.Status -eq "Online" }
Output:
Name Status
---- ------
Web01 Online
Cache01 Online
Summary
Arrays are the primary data structure for lists in PowerShell. Standard arrays work best for small fixed collections. ArrayLists handle dynamic collections efficiently. The range operator creates sequential arrays quickly. Sorting, filtering, slicing, and iteration cover all common list operations. Arrays of objects extend this to real-world scenarios like server lists, user accounts, and report data.
