Arrays¶
An array is a variable that can hold multiple values at the same time. The values are stored in a specific order, and each value is accessible by a numeric index starting from 0. Arrays are used whenever you need to work with a list of items instead of a single value.
1. Array syntax and creation¶
1.1 Comma operator¶
The simplest way to create an array is to separate values with commas:
$colors = "red", "green", "blue"
Here:
$colorsis a single variable.- It contains three elements:
"red","green","blue". - PowerShell automatically creates an array because of the commas.
You can verify that $colors is an array:
$colors.GetType().Name
This returns Object[], which means “array of objects”.
1.2 @() array literal¶
The @() syntax explicitly creates an array:
$numbers = @(10, 20, 30)
Inside @(), the values are again separated by commas. This is equivalent to the previous example, but it makes it clear that you are intentionally creating an array.
1.3 Empty array¶
$empty = @()
This creates an array with zero elements. You can later add elements to it.
2. Accessing elements¶
Array elements are accessed by index. Indexing uses square brackets [].
$colors = "red", "green", "blue"
$first = $colors[0]
$second = $colors[1]
$third = $colors[2]
$colors[0]returns"red".$colors[1]returns"green".$colors[2]returns"blue".
PowerShell also supports negative indices:
$last = $colors[-1]
$colors[-1] returns the last element of the array, here "blue". This is useful when you do not want to calculate the last index manually.
You can also select a range of elements using the range operator ..:
$subset = $colors[0..1]
$subset now contains "red" and "green".
3. Modifying arrays¶
Arrays in PowerShell are fixed‑size internally, but the language lets you work with them as if they were resizable by creating new arrays when needed.
3.1 Appending elements¶
$colors = "red", "green", "blue"
$colors += "yellow"
After the += operation:
$colorsnow contains"red","green","blue","yellow".- PowerShell created a new array that includes the original elements plus the new one.
3.2 Replacing elements¶
$colors[1] = "orange"
This replaces the element at index 1:
- Before:
"red","green","blue","yellow". - After:
"red","orange","blue","yellow".
You are not changing the size of the array, only the value stored at a specific position.
4. Iterating through arrays¶
The main reason to use arrays is to perform the same operation on each element.
4.1 foreach loop¶
$colors = "red", "green", "blue"
foreach ($color in $colors) {
Write-Output "Color: $color"
}
Explanation:
foreach ($color in $colors)takes each element of$colorsin order.- On each iteration,
$colorholds one value: first"red", then"green", then"blue". Write-Output "Color: $color"prints a line for each element.
The output is:
Color: red
Color: green
Color: blue
4.2 Pipeline with ForEach-Object¶
$colors = "red", "green", "blue"
$colors | ForEach-Object {
"Color: $_"
}
Explanation:
$colorsis sent into the pipeline.ForEach-Objectprocesses each element.$_represents the current element in the pipeline.- The script block
{ "Color: $_" }runs once per element and returns a string.
The result is the same as with foreach.
5. Arrays from command output¶
Many PowerShell commands return multiple objects. When you assign such output to a variable, that variable holds an array.
$files = Get-ChildItem
Here:
Get-ChildItemreturns one object per file or directory in the current location.$filesbecomes an array of those objects.
You can access individual items:
$firstFile = $files[0]
$firstFile.Name
$files[0]is the first file object.$firstFile.Nameprints the name of that file.
You can iterate over all files:
foreach ($file in $files) {
$file.Name
}
This prints the name of each file in the directory.
6. Typical use cases¶
- Storing a list of values to process later
$ports = 80, 443, 8080
- Performing calculations on each element
$numbers = 1, 2, 3, 4, 5
foreach ($n in $numbers) {
$square = $n * $n
"Number: $n, Square: $square"
}
- Capturing and inspecting multiple results from a command
$processes = Get-Process
$processes[0]
$processes[0].ProcessName