How to Add an Element to the Beginning of an Array in PowerShell?

When working with arrays in PowerShell, you may often need to add elements to the beginning of an array. This is an interesting topic. In this tutorial, I will explain how to add an element to the beginning of an array in PowerShell.

To add an element to the beginning of an array in PowerShell using the + operator, you can simply create a new array with the desired element and concatenate it with the existing array. For example, if you have an array $cities = @(‘Los Angeles’, ‘Chicago’, ‘Houston’) and want to add “New York” to the beginning, you can use $cities = @(‘New York’) + $cities. This will result in the updated array @(‘New York’, ‘Los Angeles’, ‘Chicago’, ‘Houston’).

Add an Element to the Beginning of an Array in PowerShell

An array in PowerShell is a data structure used to store a collection of items. Arrays can hold different types of data, such as integers, strings, and objects. Creating an array in PowerShell is straightforward. For instance:

# Creating an array
$array = 1, 2, 3, 4, 5

Now, with some examples, let me show you how to add an element to the beginning of an array in PowerShell.

You can use several methods to add an element to the beginning of an array.

Method 1: Using the + Operator

One of the simplest ways to add an element to the beginning of a PowerShell array is by using the + operator.

Syntax:

Here is the syntax:

$newArray = @('NewElement') + $array

Example:

Now, let me show you an example.

Suppose we have an array of city names, and we want to add “New York” to the beginning:

# Existing array of cities
$cities = @('Los Angeles', 'Chicago', 'Houston')

# Adding 'New York' to the beginning
$cities = @('New York') + $cities

# Output the updated array
$cities

Output:

New York
Los Angeles
Chicago
Houston

I executed the above PowerShell script, and you can see the exact output in the screenshot below:

powershell add element to array beginning

Check out Sort an Array of Objects in PowerShell

Method 2: Using the Insert() Method

Another method to add an element to the beginning of an array is by using the Insert method from the ArrayList class in PowerShell. This method is more efficient for larger arrays as it doesn’t create a new array but modifies the existing one.

Syntax:

Here is the syntax:

$arrayList.Insert(0, 'NewElement')

Example:

Let me show you an example.

Adding “Washington” to the beginning of an array of state names:

# Creating an ArrayList
$states = [System.Collections.ArrayList]@('California', 'Texas', 'Florida')

# Inserting 'Washington' at the beginning
$states.Insert(0, 'Washington')

# Output the updated array
$states

Output:

Washington
California
Texas
Florida

Here is the exact output in the screenshot below:

Add an Element to the Beginning of an Array in PowerShell

Check out Convert an ArrayList to an Array in PowerShell

Method 3: Using the Prepend() Method from LINQ

For those who prefer using LINQ, you can use the Prepend method to add an element to the beginning of an array. This method is available in PowerShell 7 and later versions.

Syntax:

Here is the syntax:

$newArray = $array.Prepend('NewElement').ToArray()

Example:

Here is an example.

Adding “Seattle” to the beginning of an array of city names:

# Existing array of cities
$cities = @('San Francisco', 'Denver', 'Miami')

# Adding 'Seattle' to the beginning using LINQ
$cities = $cities.Prepend('Seattle').ToArray()

# Output the updated array
$cities

Output:

Seattle
San Francisco
Denver
Miami

Conclusion

In this tutorial, I explained how to add an element to the beginning of an array in PowerShell using various methods. The + operator is simple and easy to use for smaller arrays, while the Insert method is more efficient for larger arrays. The Prepend method from LINQ offers a modern approach for those using PowerShell 7 and later.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.