Bird
Raised Fist0
PowerShellscripting~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using [PSCustomObject] in PowerShell?
easy
A. To convert strings into numbers
B. To create objects with named properties for organizing related data
C. To create arrays of numbers automatically
D. To run commands faster in PowerShell

Solution

  1. Step 1: Understand what [PSCustomObject] does

    [PSCustomObject] creates an object with named properties to hold related data clearly.
  2. Step 2: Compare options

    Only "To create objects with named properties for organizing related data" correctly describes this purpose. Other options describe unrelated tasks.
  3. Final Answer:

    To create objects with named properties for organizing related data -> Option B
  4. Quick Check:

    Purpose of [PSCustomObject] = Organize data [OK]
Hint: Remember: [PSCustomObject] groups data with names [OK]
Common Mistakes:
  • Thinking it speeds up command execution
  • Confusing with arrays or type conversion
  • Assuming it creates numeric sequences
2. Which of the following is the correct syntax to create a custom object with properties Name and Age in PowerShell?
easy
A. [PSCustomObject] @{Name: 'Alice'; Age: 30}
B. @{Name='Alice', Age=30} [PSCustomObject]
C. [PSCustomObject]@{Name='Alice'; Age=30}
D. [PSCustomObject]@{Name='Alice' Age=30}

Solution

  1. Step 1: Recall correct syntax for custom object creation

    The correct syntax uses [PSCustomObject]@{PropertyName='Value'; PropertyName2=Value2} with semicolons separating properties.
  2. Step 2: Analyze each option

    [PSCustomObject]@{Name='Alice'; Age=30} matches the correct syntax. @{Name='Alice', Age=30} [PSCustomObject] reverses order. [PSCustomObject] @{Name: 'Alice'; Age: 30} uses colons instead of equals. [PSCustomObject]@{Name='Alice' Age=30} misses semicolon between properties.
  3. Final Answer:

    [PSCustomObject]@{Name='Alice'; Age=30} -> Option C
  4. Quick Check:

    Correct syntax uses semicolons and equals [OK]
Hint: Use semicolons and equals inside @{ } with [PSCustomObject] [OK]
Common Mistakes:
  • Using commas instead of semicolons
  • Placing [PSCustomObject] after the hashtable
  • Using colons instead of equals signs
3. What will be the output of this PowerShell code?
$person = [PSCustomObject]@{Name='Bob'; Age=25}; $person.Name
medium
A. Bob
B. 25
C. @{Name='Bob'; Age=25}
D. Name

Solution

  1. Step 1: Understand object creation and property access

    The variable $person holds a custom object with properties Name and Age. Accessing $person.Name returns the value of the Name property.
  2. Step 2: Determine the output

    The Name property was set to 'Bob', so the output is 'Bob'.
  3. Final Answer:

    Bob -> Option A
  4. Quick Check:

    Access property returns its value [OK]
Hint: Access properties with dot notation to get their values [OK]
Common Mistakes:
  • Expecting the whole object printed
  • Confusing property name with value
  • Using wrong property name
4. Identify the error in this PowerShell code:
$obj = [PSCustomObject]@{Name='Eve' Age=28}
medium
A. Missing semicolon between properties inside the hashtable
B. Incorrect use of square brackets around PSCustomObject
C. Property names must be in double quotes
D. Cannot assign numbers as property values

Solution

  1. Step 1: Check hashtable property separators

    Properties inside the hashtable must be separated by semicolons. Here, Name='Eve' and Age=28 are missing a semicolon between them.
  2. Step 2: Verify other syntax elements

    Square brackets around PSCustomObject are correct. Property names do not require quotes. Numbers can be property values.
  3. Final Answer:

    Missing semicolon between properties inside the hashtable -> Option A
  4. Quick Check:

    Separate properties with semicolons [OK]
Hint: Separate each property with a semicolon inside @{ } [OK]
Common Mistakes:
  • Using commas instead of semicolons
  • Adding quotes around property names unnecessarily
  • Thinking numbers can't be property values
5. You want to create a list of custom objects representing books with properties Title and Author. Which PowerShell code correctly creates an array of two such objects?
hard
A. [PSCustomObject]@{Title='1984', Author='Orwell'; Title='Dune', Author='Herbert'}
B. [PSCustomObject]@{Title='1984', Author='Orwell'}, [PSCustomObject]@{Title='Dune'; Author='Herbert'}
C. @{Title='1984'; Author='Orwell'}, @{Title='Dune'; Author='Herbert'}
D. @([PSCustomObject]@{Title='1984'; Author='Orwell'}, [PSCustomObject]@{Title='Dune'; Author='Herbert'})

Solution

  1. Step 1: Understand how to create multiple custom objects in an array

    Each book must be a separate [PSCustomObject]. To create an array, wrap the objects in @( ) and separate them with commas.
  2. Step 2: Analyze each option

    @([PSCustomObject]@{Title='1984'; Author='Orwell'}, [PSCustomObject]@{Title='Dune'; Author='Herbert'}) correctly creates two separate custom objects inside an array. [PSCustomObject]@{Title='1984', Author='Orwell'}, [PSCustomObject]@{Title='Dune'; Author='Herbert'} uses comma instead of semicolon in the first hashtable, causing a syntax error. @{Title='1984'; Author='Orwell'}, @{Title='Dune'; Author='Herbert'} creates hashtables, not custom objects. [PSCustomObject]@{Title='1984', Author='Orwell'; Title='Dune', Author='Herbert'} tries to combine two books in one object incorrectly.
  3. Final Answer:

    @([PSCustomObject]@{Title='1984'; Author='Orwell'}, [PSCustomObject]@{Title='Dune'; Author='Herbert'}) -> Option D
  4. Quick Check:

    Use array @( ) with separate custom objects [OK]
Hint: Wrap multiple [PSCustomObject]s in @( ) separated by commas [OK]
Common Mistakes:
  • Using commas instead of semicolons inside hashtables
  • Using hashtables instead of custom objects
  • Combining multiple items in one object incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard