0
0
PowerShellscripting~20 mins

Adding properties to objects in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Object Property Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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
A@{Color=Blue}
BColor : Blue
CPSObject
DError: Method not found
Attempts:
2 left
💡 Hint
Think about how PowerShell displays objects with properties.
💻 Command Output
intermediate
2: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
A20
BError: Property not found
C10 * 2
DScriptBlock
Attempts:
2 left
💡 Hint
ScriptProperty runs the script block when accessed.
🔧 Debug
advanced
2: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
ANew-Object PSObject does not support adding properties.
BThe property name 'Age' is reserved and cannot be used.
CAdd-Member is not a method of the object; it is a cmdlet and must be called separately.
DThe value 30 must be a string, not an integer.
Attempts:
2 left
💡 Hint
Check how Add-Member is used in PowerShell.
💻 Command Output
advanced
2: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
A
Name : Alice
Age : 28
Country : USA
B@{Name=Alice; Age=28; Country=USA}
CAlice 28 USA
DError: Multiple properties not supported
Attempts:
2 left
💡 Hint
Format-List shows each property on its own line.
🚀 Application
expert
3: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 here
Aforeach ($key in $props) { $obj.AddMember($key, $props[$key]) }
B$obj | Add-Member -MemberType NoteProperty -Name $props.Keys -Value $props.Values
CAdd-Member -InputObject $obj -MemberType NoteProperty -Name $props -Value $props
Dforeach ($key in $props.Keys) { $obj | Add-Member -MemberType NoteProperty -Name $key -Value $props[$key] }
Attempts:
2 left
💡 Hint
You need to loop through each key-value pair and add them one by one.