0
0
PowerShellscripting~10 mins

Object arrays in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Object arrays
Create empty array
Create object
Add object to array
Repeat for each object
Use array of objects
We create objects, add them to an array, and then use the array to access all objects.
Execution Sample
PowerShell
$obj1 = [PSCustomObject]@{Name='Alice'; Age=30}
$obj2 = [PSCustomObject]@{Name='Bob'; Age=25}
$array = @($obj1, $obj2)
$array | ForEach-Object { $_.Name }
Create two objects with Name and Age, put them in an array, then print each Name.
Execution Table
StepActionVariable StateOutput
1Create object obj1 with Name='Alice', Age=30obj1 = {Name=Alice, Age=30}
2Create object obj2 with Name='Bob', Age=25obj2 = {Name=Bob, Age=25}
3Create array with obj1 and obj2array = [{Name=Alice, Age=30}, {Name=Bob, Age=25}]
4Loop over array and output Name propertyarray unchangedAlice
5Continue looparray unchangedBob
6End looparray unchanged
💡 All objects processed, loop ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
obj1undefined{Name=Alice, Age=30}{Name=Alice, Age=30}{Name=Alice, Age=30}{Name=Alice, Age=30}
obj2undefinedundefined{Name=Bob, Age=25}{Name=Bob, Age=25}{Name=Bob, Age=25}
arrayundefinedundefinedundefined[{Name=Alice, Age=30}, {Name=Bob, Age=25}][{Name=Alice, Age=30}, {Name=Bob, Age=25}]
Key Moments - 3 Insights
Why do we use [PSCustomObject] to create objects?
Using [PSCustomObject] creates structured objects with named properties, which can be stored in arrays and accessed easily, as shown in steps 1 and 2.
Can we add objects to the array after creating it?
Yes, but in this example we create the array with objects inside it at once (step 3). You can also add objects later using += operator.
How do we access properties of objects inside the array?
We loop over the array and use $_.PropertyName to access each object's property, as shown in steps 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of obj2 after step 2?
A{Name=Alice, Age=30}
Bundefined
C{Name=Bob, Age=25}
Dempty array
💡 Hint
Check the 'Variable State' column at step 2 for obj2.
At which step does the array get created with both objects inside?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the action 'Create array with obj1 and obj2' in the execution table.
If we add a third object to the array, how would the output change in the loop?
AIt would print the Name of all three objects
BIt would print only the first two Names
CIt would cause an error
DIt would print nothing
💡 Hint
Refer to how the loop outputs each object's Name property in steps 4 and 5.
Concept Snapshot
Object arrays in PowerShell:
- Create objects with [PSCustomObject]@{Property=Value}
- Store objects in an array: $array = @($obj1, $obj2)
- Access objects with loops: $array | ForEach-Object { $_.Property }
- Arrays hold multiple objects for easy management
- Use properties to get data from each object
Full Transcript
This example shows how to create objects in PowerShell using [PSCustomObject] with named properties like Name and Age. Then, we put these objects into an array. The array holds multiple objects together. We loop through the array using ForEach-Object and print the Name property of each object. Variables obj1 and obj2 hold the individual objects. The array variable holds both objects. The loop runs twice, printing 'Alice' and then 'Bob'. This method helps organize data as objects inside arrays for easy access and processing.