Bring related data together
Create an array of structured objects.
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.
$settings = @{Team='Operations'; Retries=3}
$settings['Team']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
$devices = @(
[pscustomobject]@{Name='WEB-01'; Role='Web'}
[pscustomobject]@{Name='DB-01'; Role='Database'}
)
$devicesThe array holds two objects with a consistent Name/Role structure. PowerShell sends each object into the output stream.