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, 5Now, 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') + $arrayExample:
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
$citiesOutput:
New York
Los Angeles
Chicago
HoustonI executed the above PowerShell script, and you can see the exact output in the screenshot below:

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
$statesOutput:
Washington
California
Texas
FloridaHere is the exact output in the screenshot below:

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
$citiesOutput:
Seattle
San Francisco
Denver
MiamiConclusion
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:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.