Challenge - 5 Problems
PowerShell Object Property Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output after adding a NoteProperty?
Consider this PowerShell code that creates a custom object and adds a NoteProperty. What will be the output when the object is displayed?
PowerShell
$obj = New-Object PSObject $obj | Add-Member -MemberType NoteProperty -Name 'Color' -Value 'Blue' $obj
Attempts:
2 left
💡 Hint
Think about how PowerShell displays objects with properties.
✗ Incorrect
When you add a NoteProperty to a PSObject and output the object, PowerShell shows the property name and its value in a list format.
💻 Command Output
intermediate2:00remaining
What happens when you add a ScriptProperty?
Given this code, what will be the output of `$obj.Size`?
PowerShell
$obj = New-Object PSObject $obj | Add-Member -MemberType ScriptProperty -Name 'Size' -Value { 10 * 2 } $obj.Size
Attempts:
2 left
💡 Hint
ScriptProperty runs the script block when accessed.
✗ Incorrect
A ScriptProperty runs the script block and returns its result when accessed, so `$obj.Size` evaluates to 20.
🔧 Debug
advanced2:00remaining
Why does this code fail to add a property?
This code is intended to add a NoteProperty 'Age' with value 30 to the object. Why does it fail?
PowerShell
$obj = New-Object PSObject $obj.Add-Member -MemberType NoteProperty -Name 'Age' -Value 30
Attempts:
2 left
💡 Hint
Check how Add-Member is used in PowerShell.
✗ Incorrect
Add-Member is a cmdlet that takes the object as input; it is not a method of the object instance.
💻 Command Output
advanced2:00remaining
What is the output after adding multiple properties?
What will be the output of this script?
PowerShell
$obj = New-Object PSObject $obj | Add-Member -MemberType NoteProperty -Name 'Name' -Value 'Alice' $obj | Add-Member -MemberType NoteProperty -Name 'Age' -Value 28 $obj | Add-Member -MemberType NoteProperty -Name 'Country' -Value 'USA' $obj | Format-List
Attempts:
2 left
💡 Hint
Format-List shows each property on its own line.
✗ Incorrect
Format-List outputs each property and its value on separate lines in the format 'Name : Value'.
🚀 Application
expert3:00remaining
How to dynamically add properties from a hashtable?
You have a hashtable `$props = @{City='Seattle'; Zip=98101}`. Which script correctly adds these as NoteProperties to a new PSObject `$obj`?
PowerShell
$props = @{City='Seattle'; Zip=98101}
$obj = New-Object PSObject
# Add properties hereAttempts:
2 left
💡 Hint
You need to loop through each key-value pair and add them one by one.
✗ Incorrect
Option D loops through each key in the hashtable and adds a NoteProperty with the key as name and value from the hashtable. Other options misuse Add-Member or syntax.