How To Create an Array Of Objects In PowerShell?

Creating an array of objects in PowerShell is easy, and in this blog post, I will guide you through how to create an array of objects in PowerShell and how to manipulate the PowerShell array of objects.

To create an array of objects in PowerShell, you can use a command like $myArray = @([pscustomobject]@{Property1='Value1'; Property2='Value2'}, [pscustomobject]@{Property1='Value3'; Property2='Value4'}), which initializes an array with two custom objects. If you need to append objects to an existing array, you can use $myArray += [pscustomobject]@{Property1='Value5'; Property2='Value6'} to add a new object.

What is an Array of Objects in PowerShell?

In PowerShell, an array is a data structure that can hold a collection of items. These items can be of any data type, including integers, strings, and objects. An array of objects, therefore, is a collection where each item within the array is an object.

An object in PowerShell is an instance of a .NET class that can contain properties and methods. Objects are represented by properties (which hold data) and methods (which perform actions). PowerShell objects are typically created using custom classes or by using the built-in PSCustomObject type.

Create an Array of Objects in PowerShell

Now, let us see how to create an array of objects in PowerShell.

Create a Single Object

To create an object in PowerShell, you can use the New-Object cmdlet or cast a hashtable to a PSCustomObject. Here’s a simple example of creating one object with custom properties in PowerShell:

# Using New-Object
$person = New-Object -TypeName PSObject -Property @{
    FirstName = "John"
    LastName = "Doe"
    Age = 30
}

# Using PSCustomObject
$person = [PSCustomObject]@{
    FirstName = "John"
    LastName = "Doe"
    Age = 30
}

# Output the object
$person

Both examples create an object representing a person with first name, last name, and age. The second method is more concise and preferred in modern PowerShell scripting.

Creating an Array of Objects

Now, let’s extend this concept to create multiple objects and store them in an array in PowerShell. An array of objects is simply a collection where each item is an object.

Here is an example of creating an array of multiple objects in PowerShell.

# Define custom objects and create an array
$objectArray = @(
    [PSCustomObject]@{
        FirstName = "John"
        LastName = "Doe"
        Age = 30
    },
    [PSCustomObject]@{
        FirstName = "Jane"
        LastName = "Smith"
        Age = 25
    }
)

# Output the array
$objectArray

In this script, we create two objects and enclose them in an array notation @(), which tells PowerShell to treat this as an array. Each object is created using the [PSCustomObject] type cast and has a hashtable defining its properties.

I executed the script using Visual Studio Code; you can see the screenshot below for reference.

Create an Array Of Objects In PowerShell

Access Data in an Array of Objects in PowerShell

Once you have an array of objects, you can access individual objects just like you would with any other array, using their index in PowerShell. Here is the complete example.

# Define custom objects and create an array
$objectArray = @(
    [PSCustomObject]@{
        FirstName = "John"
        LastName = "Doe"
        Age = 30
    },
    [PSCustomObject]@{
        FirstName = "Jane"
        LastName = "Smith"
        Age = 25
    }
)

# Access the first object in the array
$firstObject = $objectArray[0]

# Output the first object
$firstObject

You can also iterate over the PowerShell array using a loop, which is a common practice when dealing with arrays:

# Define custom objects and create an array
$objectArray = @(
    [PSCustomObject]@{
        FirstName = "John"
        LastName = "Doe"
        Age = 30
    },
    [PSCustomObject]@{
        FirstName = "Jane"
        LastName = "Smith"
        Age = 25
    }
)

# Loop through each object in the array
foreach ($obj in $objectArray) {
    # Output the current object
    $obj
}

Look the screenshot below, I have executed the PowerShell script.

How To Create an Array Of Objects In PowerShell

Modifying Objects in a PowerShell Array

If you need to modify the properties of an object within an array in PowerShell, you can do so by accessing the object by its index and then changing the property value:

# Define custom objects and create an array
$objectArray = @(
    [PSCustomObject]@{
        FirstName = "John"
        LastName = "Doe"
        Age = 30
    },
    [PSCustomObject]@{
        FirstName = "Jane"
        LastName = "Smith"
        Age = 25
    }
)

# Change the age of the first object
$objectArray[0].Age = 31

# Output the modified array
$objectArray

Adding and Removing Objects from an Array

To add a new object to an existing array, you can use the += operator in PowerShell:

# Define custom objects and create an array
$objectArray = @(
    [PSCustomObject]@{
        FirstName = "John"
        LastName = "Doe"
        Age = 30
    },
    [PSCustomObject]@{
        FirstName = "Jane"
        LastName = "Smith"
        Age = 25
    }
)

# Add a new object to the array
$objectArray += [PSCustomObject]@{
    FirstName = "Alice"
    LastName = "Johnson"
    Age = 28
}

# Output the updated array
$objectArray

To remove an object, you can use the Where-Object cmdlet to filter out the object you want to remove:

# Define custom objects and create an array
$objectArray = @(
    [PSCustomObject]@{
        FirstName = "John"
        LastName = "Doe"
        Age = 30
    },
    [PSCustomObject]@{
        FirstName = "Jane"
        LastName = "Smith"
        Age = 25
    }
)

# Remove the object with FirstName 'Jane'
$objectArray = $objectArray | Where-Object { $_.FirstName -ne 'Jane' }

# Output the updated array
$objectArray

Conclusion

Creating an array of objects in PowerShell is easy by using PSCustomObject. By using arrays of objects, you can manage and manipulate complex data structures with ease. I hope now you understand how to work with arrays of objects in PowerShell, especially, how to create an array of objects in PowerShell.

You may also like: