Bird
Raised Fist0
PowerShellscripting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the Add-Member cmdlet do in PowerShell when working with objects?
easy
A. It converts an object to a string.
B. It adds new properties or methods to an existing object.
C. It creates a new object from scratch.
D. It deletes properties from an object.

Solution

  1. Step 1: Understand Add-Member purpose

    Add-Member is used to add new properties or methods to an existing object without creating a new object.
  2. Step 2: Compare options

    Options A, B, and C describe converting, deleting, or creating objects, which are not the purpose of Add-Member.
  3. Final Answer:

    It adds new properties or methods to an existing object. -> Option B
  4. Quick Check:

    Add-Member adds properties/methods [OK]
Hint: Add-Member adds properties or methods to existing objects [OK]
Common Mistakes:
  • Thinking Add-Member creates new objects
  • Confusing Add-Member with Remove-Item
  • Assuming Add-Member converts objects to strings
2. Which of the following is the correct syntax to add a NoteProperty named Age with value 30 to an existing object $person?
easy
A. Add-Member -Object $person -Type NoteProperty -Property Age -Value 30
B. Add-Member -InputObject $person -MemberType NoteProperty -Property Age -Value 30
C. Add-Member -InputObject $person -MemberType Property -Name Age -Value 30
D. Add-Member -InputObject $person -MemberType NoteProperty -Name Age -Value 30

Solution

  1. Step 1: Identify correct parameter names

    The correct parameters for Add-Member are -InputObject, -MemberType, -Name, and -Value.
  2. Step 2: Check member type and property name

    NoteProperty is the correct member type for adding simple properties. The property name is specified with -Name.
  3. Final Answer:

    Add-Member -InputObject $person -MemberType NoteProperty -Name Age -Value 30 -> Option D
  4. Quick Check:

    Correct syntax uses -InputObject, -MemberType, -Name, -Value [OK]
Hint: Use -InputObject, -MemberType NoteProperty, -Name, and -Value [OK]
Common Mistakes:
  • Using -Object instead of -InputObject
  • Using -Property instead of -Name
  • Using MemberType Property instead of NoteProperty
3. 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
medium
A. Red
B. Color
C. PSObject
D. Error: Property not found

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]
Hint: 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
4. Identify the error in this PowerShell script that tries to add a property Height to $obj:
$obj = New-Object PSObject
Add-Member -InputObject $obj -MemberType NoteProperty -Property Height -Value 180
Write-Output $obj.Height
medium
A. The value 180 must be a string, not a number.
B. The object $obj is not created properly.
C. The parameter '-Property' should be '-Name' when adding a property.
D. Write-Output cannot access properties added by Add-Member.

Solution

  1. Step 1: Check Add-Member parameters

    The parameter to specify the property name is -Name, not -Property. Using -Property causes an error.
  2. Step 2: Confirm object creation and output

    $obj is correctly created. The value 180 can be a number. Write-Output can access added properties if correctly added.
  3. Final Answer:

    The parameter '-Property' should be '-Name' when adding a property. -> Option C
  4. Quick Check:

    Use -Name to specify property name in Add-Member [OK]
Hint: Use -Name, not -Property, to name new properties [OK]
Common Mistakes:
  • Using -Property instead of -Name
  • Thinking numeric values must be strings
  • Assuming Write-Output can't show added properties
5. You have an object $car with properties Make and Model. You want to add a new property Year only if it does not already exist. Which script correctly does this?
hard
A. if (-not $car.PSObject.Properties.Name -contains 'Year') { Add-Member -InputObject $car -MemberType NoteProperty -Name Year -Value 2023 }
B. if ($car.Year -eq $null) { Add-Member -InputObject $car -MemberType NoteProperty -Name Year -Value 2023 }
C. Add-Member -InputObject $car -MemberType NoteProperty -Name Year -Value 2023 -Force
D. Add-Member -InputObject $car -MemberType NoteProperty -Name Year -Value 2023

Solution

  1. Step 1: Check if property 'Year' exists

    Using $car.PSObject.Properties.Name -contains 'Year' checks if the property exists. The -not negates it to add only if missing.
  2. Step 2: Add property conditionally

    If 'Year' does not exist, Add-Member adds it with value 2023. This avoids errors from duplicate properties.
  3. Final Answer:

    if (-not $car.PSObject.Properties.Name -contains 'Year') { Add-Member -InputObject $car -MemberType NoteProperty -Name Year -Value 2023 } -> Option A
  4. Quick Check:

    Check property existence before adding to avoid duplicates [OK]
Hint: Check property names with $obj.PSObject.Properties.Name before adding [OK]
Common Mistakes:
  • Adding property without checking existence causes errors
  • Using -Force blindly overwrites properties
  • Checking property value instead of existence
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard