PowerShell by Patrik

Creating an Array of Objects

To construct an array of objects in PowerShell, each item within the array is inherently treated as an object. Unlike conventional arrays that may consist of strings or integers, PowerShell arrays primarily contain objects. Below is a structured example demonstrating the creation of an array comprising objects through explicit declaration:

$people = @(
    [PSCustomObject]@{Name='John'; Age=26},
    [PSCustomObject]@{Name='Jane'; Age=22}
)

In this illustration, $people is an array variable containing two objects. Each object is defined using the [PSCustomObject] syntax followed by property-value pairs enclosed within curly braces {}. This structured approach ensures clarity and ease of comprehension while creating arrays of objects in PowerShell.

Comments

Leave a Comment

All fields are required. Your email address will not be published.