A PSCustomObject is a way to create custom objects with properties and values in PowerShell. It helps organize data like a simple table or record.
Click to reveal answer
beginner
How do you create a PSCustomObject with properties Name and Age?
Use the syntax: $obj = [PSCustomObject]@{Name='Alice'; Age=30} This creates an object with two properties: Name and Age.
Click to reveal answer
intermediate
Why use PSCustomObject instead of a hashtable?
PSCustomObject lets you create objects that behave like real objects with properties, making it easier to work with in scripts and output. Hashtables are just key-value pairs without object behavior.
Click to reveal answer
intermediate
How can you add a new property to an existing PSCustomObject?
You can add a property by using Add-Member cmdlet, for example: $obj | Add-Member -NotePropertyName 'City' -NotePropertyValue 'Seattle'
Click to reveal answer
beginner
What will this script output? $person = [PSCustomObject]@{Name='Bob'; Age=25} $person.Age = 26 $person
It outputs the object with updated Age property: Name Age ---- --- Bob 26 This shows you can change property values after creation.
Click to reveal answer
Which syntax correctly creates a PSCustomObject with a property 'Color' set to 'Red'?
A$obj = @{Color='Red'}
B$obj = [Object]@{Color='Red'}
C$obj = New-Object Color='Red'
D$obj = [PSCustomObject]@{Color='Red'}
✗ Incorrect
Option D uses the correct syntax to create a PSCustomObject with the property Color set to Red.
How do you add a new property 'Country' with value 'USA' to an existing PSCustomObject $obj?
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.
Step 2: Determine the output
The Name property was set to 'Bob', so the output is 'Bob'.
Final Answer:
Bob -> Option A
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
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.
Step 2: Verify other syntax elements
Square brackets around PSCustomObject are correct. Property names do not require quotes. Numbers can be property values.
Final Answer:
Missing semicolon between properties inside the hashtable -> Option A
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
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.
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.
Final Answer:
@([PSCustomObject]@{Title='1984'; Author='Orwell'}, [PSCustomObject]@{Title='Dune'; Author='Herbert'}) -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.