How To Create Empty Array Of Objects In PowerShell?

Do you want to create an empty array of objects in PowerShell? In this PowerShell tutorial, I have explained how to create an empty array of objects in PowerShell.

To create an empty array of objects in PowerShell by initializing an array with the array subexpression operator @(). For example, $emptyArray = @() gives you an empty array to which you can add objects using $emptyArray += New-Object -TypeName PSObject. This approach is straightforward and suitable for beginners to start manipulating arrays in PowerShell.

Create Empty Array Of Objects In PowerShell

The simplest way to create an empty array in PowerShell is by using the array subexpression operator @(). This operator initializes a new array that you can then add objects to.

# Create an empty array
$array = @()

This array is now ready to store objects, though it currently contains none.

Adding Objects to an Empty Array

Once you have an empty array, you can add objects to it in PowerShell. There are a couple of ways to do this.

Using the += Operator

You can add an object to an array using the += operator. This operator appends an item to the end of the array.

# Add a new object to the array
$array += New-Object -TypeName PSObject

This example creates a new PSObject, which is a generic object type in PowerShell, and adds it to the array.

Using the .Add() Method with ArrayList

If you plan to add many items to an array, it’s more efficient to use an ArrayList or a generic list because these types of lists are designed for frequent modifications.

# Create an ArrayList
$arrayList = New-Object System.Collections.ArrayList

# Add a new object to the ArrayList
$null = $arrayList.Add((New-Object -TypeName PSObject))

Note that we assign the result of $arrayList.Add() to $null because the .Add() method returns the index of the added item, which we usually don’t need.

Create an Array of Arrays in PowerShell

Sometimes, you may need an array where each item is itself an array. This is known as a jagged array.

# Create an empty array of arrays
$arrayOfArrays = @()

To add a new array to this array of arrays, you use the += operator with a comma to indicate that the item being added is itself an array.

# Add a new empty array to the array of arrays
$arrayOfArrays += ,@()

Creating an Array of Custom Objects in PowerShell

You can also create an array to hold custom objects in PowerShell. Custom objects are instances of PSObject that have been extended with additional properties.

# Create an empty array for custom objects
$customObjectArray = @()

# Define a new custom object and add it to the array
$obj = New-Object -TypeName PSObject -Property @{
    Name = 'John Doe'
    Age = 30
}
$customObjectArray += $obj
Write-Host $customObjectArray

You can see the output in the screenshot below, after I ran the code using PowerShell ISE.

powershell create empty array of objects

Using a Loop to Populate an Array

Loops are often used to populate arrays. For example, you can use a foreach loop to iterate over a set of values and add an object to an array for each value.

# Create an empty array
$users = @()

# Simulate adding multiple user objects to the array
foreach ($i in 1..5) {
    $user = New-Object -TypeName PSObject -Property @{
        ID = $i
        Username = "User$i"
    }
    $users += $user
}

Conclusion

In this PowerShell tutorial, I have explained how to create an empty array of objects in PowerShell.

You may also like: