0
0
PowerShellscripting~20 mins

Custom objects (PSCustomObject) in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PSCustomObject Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this PowerShell code creating a PSCustomObject?

Consider the following PowerShell script:

$obj = [PSCustomObject]@{Name='Alice'; Age=30; City='Seattle'}
$obj | Format-List

What will be the output shown in the console?

PowerShell
$obj = [PSCustomObject]@{Name='Alice'; Age=30; City='Seattle'}
$obj | Format-List
AName: Alice, Age: 30, City: Seattle
B
Name : Alice
Age  : 30
City : Seattle
CName=Alice; Age=30; City=Seattle
D@{Name=Alice; Age=30; City=Seattle}
Attempts:
2 left
💡 Hint

Think about how Format-List displays properties of an object in PowerShell.

🧠 Conceptual
intermediate
1:30remaining
Which statement about PSCustomObject property types is true?

In PowerShell, when you create a PSCustomObject with properties, which of the following is true about the property types?

AAll properties are always strings regardless of assigned value.
BProperties are stored as integers by default.
CProperties keep the data type of the assigned value.
DProperties cannot hold arrays or nested objects.
Attempts:
2 left
💡 Hint

Think about how PowerShell handles variable types when assigning values.

🔧 Debug
advanced
2:00remaining
Why does this PSCustomObject creation script fail?

Examine this PowerShell code snippet:

$obj = [PSCustomObject]@{Name='Bob' Age=25 City='Boston'}

Why does this script produce an error?

AHashtable keys must be integers, not strings.
BPSCustomObject cannot be created with string values.
CThe variable $obj is reserved and cannot be used.
DMissing semicolons between hashtable key-value pairs cause a syntax error.
Attempts:
2 left
💡 Hint

Check the syntax rules for hashtables in PowerShell.

🚀 Application
advanced
2:00remaining
How to add a new property to an existing PSCustomObject?

You have this PSCustomObject:

$person = [PSCustomObject]@{Name='Eve'; Age=28}

Which command correctly adds a new property City with value "Denver" to $person?

A$person | Add-Member -MemberType NoteProperty -Name City -Value 'Denver'
B$person.City = 'Denver'
C$person += @{City='Denver'}
D$person = $person + @{City='Denver'}
Attempts:
2 left
💡 Hint

Think about how to add properties to objects in PowerShell without recreating them.

💻 Command Output
expert
2:00remaining
What is the output of this script creating nested PSCustomObjects?

Consider this PowerShell script:

$address = [PSCustomObject]@{Street='123 Maple St'; City='Austin'}
$person = [PSCustomObject]@{Name='John'; Age=40; Address=$address}
$person.Address.City

What will be the output?

PowerShell
$address = [PSCustomObject]@{Street='123 Maple St'; City='Austin'}
$person = [PSCustomObject]@{Name='John'; Age=40; Address=$address}
$person.Address.City
AAustin
BError: Property 'City' not found
CJohn
D123 Maple St
Attempts:
2 left
💡 Hint

Think about how nested objects work and how to access their properties.