Bird
0
0

What will be the output of this PowerShell code?

medium📝 Command Output Q13 of 15
PowerShell - Working with Objects
What will be the output of this PowerShell code?
$obj = New-Object PSObject
Add-Member -InputObject $obj -MemberType NoteProperty -Name Color -Value 'Red'
Write-Output $obj.Color
ARed
BColor
CPSObject
DError: Property not found
Step-by-Step Solution
Solution:
  1. Step 1: Add NoteProperty 'Color' with value 'Red'

    The Add-Member cmdlet adds a property named Color with value 'Red' to the object $obj.
  2. Step 2: Output the property value

    Write-Output $obj.Color accesses the added property and prints its value, which is Red.
  3. Final Answer:

    Red -> Option A
  4. Quick Check:

    Access added property with dot notation [OK]
Quick Trick: Access added properties with dot notation like $obj.Property [OK]
Common Mistakes:
  • Expecting property name instead of value
  • Forgetting to add the property before accessing
  • Using wrong property name in output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes