Powershell

How to Initialize an Array of Custom Objects

PowerShell supports different data types, such as integers, variables, strings, or arrays. More specifically, an array is a data structure and a collection of various elements. In an array, each element has an index number. Those elements are selected and called using their index number after declaration. Arrays with custom objects can also be created using PowerShell. PowerShell custom objects are the objects that we usually create to get or retrieve the data we want to collect.

This post has elaborated a procedural guide on how to initialize an array.

How to Initialize an Array of Custom Objects?

These methods can be utilized to initialize an array of objects in PowerShell:

    • Initialize a single array of custom objects.
    • Initialize an array of multiple custom objects.

Method 1: Initializing an Array of Single Custom Objects in PowerShell

The first method is to initialize an array of custom objects using a single array along with the “[PSCustomObject]” keyword at the start of an array. It is used to create an array in a very structured manner.

Example

In this example, we will initialize a single array of custom objects:

$Employees = [PSCustomObject]@{
    Name = "James"
    Profession = "Doctor"
    Age = "29"
}

 
According to the above code:

    • First of all, create an array of custom objects and assign it to the “$Employees” variable.
    • Inside an array, specify multiple properties and assign values to them:

 

Let’s test the creation of an array of custom objects by accessing the required property of the created array as follows:

> $Employees.Name

 

It can be observed that the value of the “Name” property has been retrieved as “James”.

Method 2: Initializing an Array of Multiple Custom Objects in PowerShell

An array can also be initialized with multiple custom objects. In this type of array, the same properties in different objects contain different values. These multiple values in different objects can be invoked by calling their property name.

Example

In this given example, we will create multiple arrays of custom objects:

> $People = @(
>       [pscustomobject]@{Name='James'; Profession='Doctor'; Age='27'}
>       [pscustomobject]@{Name='Emma'; Profession='Nurse'; Age='23'}
>       [pscustomobject]@{Name='Peter'; Profession='Plumber'; Age='28'}
>       )

 

For the verification, retrieve the value of any of the required property:

> $People.Profession

 

As you can see, the profession of the created multiple objects has been fetched and displayed successfully.

Conclusion

An array can be initialized by using the “@()” structure. The array items are passed inside the parentheses. More specifically, an array of custom objects can be initialized in PowerShell using a single array of custom objects and also multiple arrays of custom objects. This write-up has overviewed a detailed procedural guide to initialize an array of objects in PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.