0
0
PowerShellscripting~10 mins

Custom objects (PSCustomObject) in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom objects (PSCustomObject)
Start script
Create PSCustomObject
Add properties with values
Use object (e.g., output or assign)
End script
The script starts by creating a PSCustomObject, adds properties with values, then uses the object, and finally ends.
Execution Sample
PowerShell
$person = [PSCustomObject]@{
  Name = "Alice"
  Age = 30
}
$person
Creates a custom object with Name and Age properties, then outputs it.
Execution Table
StepActionEvaluationResult
1Create PSCustomObject with properties Name and Age$person = [PSCustomObject]@{Name="Alice"; Age=30}Object with Name='Alice', Age=30 created
2Output $person$personDisplays: Name : Alice Age : 30
3End of scriptNo further commandsScript ends
💡 Script ends after outputting the custom object
Variable Tracker
VariableStartAfter Step 1After Step 2Final
$personnullPSCustomObject(Name='Alice', Age=30)Same objectSame object
Key Moments - 2 Insights
Why do we use [PSCustomObject]@{} instead of just a hashtable?
Using [PSCustomObject]@{} creates a real object with properties, not just a hashtable. This allows easier property access and better output formatting, as shown in Step 1 and Step 2 of the execution_table.
Can we add properties after creating the PSCustomObject?
No, PSCustomObjects are fixed after creation. To add properties, you must create a new object or use other methods. This is why all properties are defined together in Step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $person after Step 1?
ANull
BA hashtable with keys Name and Age
CAn object with Name='Alice' and Age=30
DAn empty object
💡 Hint
Check the 'Result' column in Step 1 of the execution_table
At which step does the script output the custom object?
AStep 1
BStep 2
CStep 3
DNo output in script
💡 Hint
Look at the 'Action' and 'Result' columns in the execution_table
If you add a new property after creating $person, what happens?
AAn error occurs because PSCustomObject is fixed
BThe object resets to empty
CThe property is added successfully
DThe property is ignored silently
💡 Hint
Refer to the second key_moment about adding properties after creation
Concept Snapshot
Create custom objects with [PSCustomObject]@{Property=Value; ...}
Properties are fixed at creation time.
Access properties with dot notation: $obj.Property
Outputs show properties clearly.
Useful for structured data in scripts.
Full Transcript
This script shows how to create a custom object in PowerShell using PSCustomObject. First, we create an object with properties Name and Age set to 'Alice' and 30. Then we output the object, which displays its properties clearly. The object is stored in the variable $person. PSCustomObjects are fixed after creation, so all properties must be defined together. This method is better than using a hashtable because it provides easier property access and nicer output formatting.