02 / WORKING WITH DATA · LESSON 05

Bring related data together

Create an array of structured objects.

10 min · learn at your pace

One value, then many

An array holds a sequence of values. @() makes an array explicitly, and a comma separates its items:

$names = @('WEB-01', 'DB-01')
$names[0]
$names.Count

Indexes begin at zero, so $names[0] is WEB-01.

A hashtable stores values under keys. Its syntax uses @{}:

$settings = @{Team='Operations'; Retries=3}
$settings['Team']

Build a tiny inventory

Convert a hashtable into a custom object when you want a consistent record with properties:

$devices = @(
    [pscustomobject]@{Name='WEB-01'; Role='Web'}
    [pscustomobject]@{Name='DB-01'; Role='Database'}
)
$devices

Each record has the same shape. This structure makes filtering, sorting, and exporting straightforward. An array groups records; each object describes one record. Keep that distinction in mind as your scripts grow.

Try an example.ps1
$settings = @{Team='Operations'; Retries=3}
$settings['Team']
YOUR TURN

Put it into practice.

Create and output an array of two custom objects: WEB-01 with Role Web, and DB-01 with Role Database. Each object needs Name and Role properties.

See an explained solution
One way to solve it.ps1
$devices = @(
    [pscustomobject]@{Name='WEB-01'; Role='Web'}
    [pscustomobject]@{Name='DB-01'; Role='Database'}
)
$devices

The array holds two objects with a consistent Name/Role structure. PowerShell sends each object into the output stream.