Arrays Are like In-Memory Database Tables
Arrays are fundamental to PowerShell scripting. They allow you to store and manipulate collections of objects, making them essential for handling data in scripts. Think of arrays as in-memory database tables where each element is a row, and you can perform operations like filtering, sorting, and slicing.
1. What is an Array?
An array is a collection of objects. Unlike a scalar (a single object), an array can hold zero, one, or many objects. Arrays are sometimes called collections or lists.
2. Creating Arrays
Empty Array
To create an empty array:
$array = @()
Array with Elements
To create an array with elements, separate them with commas:
$array = @(1, "hello", 433.2, "world")
The @()
syntax ensures the variable is treated as an array, even if it contains only one element.
Implicit Array Creation
You can also create arrays without explicitly using @()
:
$array = 1, "hello", 433.2, "world"
Range Operator
To create an array with a sequence of numbers:
$x = 0..20000 # Array from 0 to 20000
$y = -2..150 # Array from -2 to 150
$z = 58..50058 # Array from 58 to 50058
Single-Element Array
To create an array with a single element:
$array1 = ,"Hello" # Using a comma
$array2 = @("World") # Using @()
Appending to an Array
To add elements to an existing array:
$array += "morestuff"
3. Accessing Array Elements
Counting Elements
To get the number of elements in an array:
$array.Count
Displaying Elements
To display all elements:
$array
To display elements as a single string (space-separated):
[string]$array
To display elements as a comma-separated string:
$array -join ","
Accessing Specific Elements
Array elements are indexed starting from 0
. Use square brackets []
to access elements:
$array[0] # First element
$array[2] # Third element
$array[-1] # Last element
$array[-2] # Second-to-last element
Slicing Arrays
To retrieve a range of elements:
$array[0..30] # Elements from index 0 to 30
$array[-1..-10] # Last 10 elements (in reverse order)
To extract specific elements:
$array[0, -1] # First and last elements
Assigning Elements to Variables
To extract elements and assign them to variables:
$v, $w, $x, $y, $z = $array[2, 30, 997, -32, -1]
4. Nested Arrays (Array of Arrays)
A nested array is an array that contains other arrays. This is useful for representing multi-dimensional data.
Creating Nested Arrays
$array1 = 10, 11
$array2 = 20, 22
$arrayAA = $array1, $array2 # Nested array
$array4 = 40, 44
$array5 = 50, 55
$arrayBB = $array4, $array5 # Another nested array
$bigarray = $arrayAA, $arrayBB # Multi-level nested array
Accessing Nested Array Elements
To access elements in a nested array:
$bigarray[0][0][0] # 10 (First element of the first nested array)
$bigarray[1][1][1] # 55 (Second element of the second nested array)
$bigarray[1][0][1] # 44 (Second element of the first nested array in the second level)
5. Manipulating Arrays
Filtering Arrays
Use Where-Object
to filter arrays:
$filteredArray = $array | Where-Object { $_ -gt 100 } # Filter elements greater than 100
Sorting Arrays
Use Sort-Object
to sort arrays:
$sortedArray = $array | Sort-Object # Sort in ascending order
Adding and Removing Elements
Add: Use
+=
to append elements.Remove: Use
Where-Object
to exclude elements.
Example:
$array += "newElement" # Add an element
$array = $array | Where-Object { $_ -ne "removeThis" } # Remove an element
6. Advanced Array Features
Multidimensional Arrays
PowerShell doesn’t natively support multidimensional arrays, but you can simulate them using nested arrays:
$matrix = @(
@(1, 2, 3),
@(4, 5, 6),
@(7, 8, 9)
)
$matrix[1][2] # Access element at row 1, column 2 (value: 6)
Array Slicing with Ranges
Use ranges to slice arrays dynamically:
$slice = $array[10..20] # Elements from index 10 to 20
Output Field Separator ($OFS
)
The $OFS
variable controls the separator used when converting arrays to strings:
$OFS = " | "
[string]$array # Elements separated by " | "
Last updated