I have been working with PowerShell for the last 12 – 15 years. One thing I use frequently is arrays in PowerShell. As a PowerShell developer, you should know about PowerShell arrays.
In this tutorial, I will explain the PowerShell array, including its definition, syntax, examples, and more.
Arrays in PowerShell are powerful data structures that store collections of items. They can hold different types of data in a single structure.
What Is a PowerShell Array?
An array in PowerShell is a data structure that stores a collection of items. PowerShell arrays can contain different types of data in the same array.
Think of it as a shopping list or a container that can hold multiple values at once. The beauty of PowerShell arrays is that they allow you to work with groups of related items as a single unit.
Here’s how you create a simple array in PowerShell:
$fruits = @("Apple", "Banana", "Cherry")
In this example, I’ve created an array called $fruits that contains three string values. The @() syntax tells PowerShell we’re creating an array.
When you run this command, PowerShell allocates memory to store these three strings together, allowing you to perform operations on all of them at once.
The array can also contain different types, like below:
$myArray = @(1, "hello", 3.14, $true)
Create PowerShell Arrays using Different Methods
There are several ways to create arrays in PowerShell. Let me show you the most common methods to create an array in PowerShell.
Method 1: Using the Array Operator @()
The array operator is the most explicit way to define an array in PowerShell:
$states = @("California", "Texas", "Florida", "New York")
This creates a clear, defined array with specific elements. The @() syntax is unmistakable and makes your intention clear. This approach is particularly useful when you want to ensure that PowerShell treats the collection as an array, even if it contains only one item.
You can also create arrays with different data types:
$mixedArray = @("John", 42, $true, (Get-Date))
This array contains a string, a number, a boolean value, and a date object – demonstrating PowerShell’s flexibility with arrays.
Method 2: Using Comma Separation
You can also create arrays in PowerShell by simply separating values with commas:
$numbers = 1, 2, 3, 4, 5
PowerShell automatically recognizes this as an array. This method is useful when you need to define an array quickly. Behind the scenes, PowerShell still creates a proper array object, just with less syntax.
Let’s verify that this is truly an array:
$numbers.GetType().Name # Returns "Object[]" confirming it's an array
Method 3: Create an Empty Array
Sometimes, you need to start with an empty array and add items later. Here is the syntax to create an empty array in PowerShell.
$employees = @()
This creates an empty array that you can populate as your script runs. This approach is valuable when you don’t know in advance what items will go into the array, such as when collecting results from a dynamic process.
For example, you might populate it within a loop:
foreach ($server in $serverList) {
if (Test-Connection $server -Quiet) {
$employees += $server # Add responsive servers to the array
}
}
Method 4: Range Operator
The range operator (..) is incredibly useful for sequential numbers. Here’s how to use it to create an array in PowerShell.
$oneToTen = 1..10
This creates an array containing the numbers 1 through 10. The range operator is a powerful shortcut that saves you from having to manually type each number. It works for any sequential integers, including negative numbers and reverse ranges:
$countdown = 5..1 # Creates [5, 4, 3, 2, 1]
$negativeRange = -3..3 # Creates [-3, -2, -1, 0, 1, 2, 3]
Next, you should know how to access array elements in PowerShell.
Check out Read CSV into Array in PowerShell
Access Array Elements in PowerShell
Now that you know how to create arrays, let’s look at how to access the data within a TypeScript array.
Here are a few examples.
Using Index Numbers
Arrays in PowerShell are zero-indexed, meaning the first item is at position 0:
$cities = @("Chicago", "Los Angeles", "Miami", "Boston")
$cities[0] # Returns "Chicago"
$cities[2] # Returns "Miami"
This zero-based indexing is standard across many programming languages. Think of the index as the “offset” from the beginning of the array. The first element is 0 positions away from the start, the second is 1 position away, and so on.
Here is the output in the screenshot below:

If you try to access an index that doesn’t exist, PowerShell will return nothing rather than an error:
$cities[10] # Returns nothing, as this index doesn't exist
Access Multiple Elements
You can retrieve multiple elements from a TypeScript array using a range. Here is how:
$cities = @("Chicago", "Los Angeles", "Miami", "Boston")
$cities[1..3] # Returns "Los Angeles", "Miami", and "Boston"
This technique is called “slicing” and allows you to extract a portion of the array. When you use the range operator with array indices, PowerShell creates a new array containing only those elements. This is extreme