Bird
0
0

What will this code output?

medium📝 Command Output Q5 of 15
PowerShell - Working with Objects
What will this code output?
$obj = New-Object PSObject
Add-Member -InputObject $obj -MemberType NoteProperty -Name Number -Value 10
Add-Member -InputObject $obj -MemberType NoteProperty -Name Number -Value 20 -Force
$obj.Number
A10
BNull
CError: Property already exists
D20
Step-by-Step Solution
Solution:
  1. Step 1: Add NoteProperty 'Number' with value 10

    The first Add-Member adds Number=10 to $obj.
  2. Step 2: Add NoteProperty 'Number' again with value 20 using -Force

    The -Force parameter overwrites the existing property with the new value 20.
  3. Step 3: Access $obj.Number

    Since the property was overwritten, $obj.Number returns 20.
  4. Final Answer:

    20 -> Option D
  5. Quick Check:

    -Force overwrites property = 20 [OK]
Quick Trick: Use -Force to overwrite existing properties [OK]
Common Mistakes:
  • Expecting error on duplicate property
  • Ignoring -Force effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes