Can you add properties directly by assignment like $object.NewProp = 'value' in PowerShell?
Yes, if the object supports it (like a custom object), you can add properties by direct assignment, e.g., $object.NewProp = 'value'.
Click to reveal answer
intermediate
What is the difference between Add-Member and direct property assignment in PowerShell?
Add-Member explicitly adds a property to an object and is useful for objects that don't support direct assignment. Direct assignment is simpler but works only on objects that allow dynamic properties.
Click to reveal answer
beginner
Why might you use New-Object PSObject -Property @{} when creating objects for adding properties?
Because PSObject supports adding properties dynamically, making it easy to create flexible objects and add properties later.
Click to reveal answer
Which cmdlet is commonly used to add a new property to an existing PowerShell object?
ASet-Property
BAdd-Member
CNew-Property
DAdd-Property
✗ Incorrect
Add-Member is the correct cmdlet to add properties to objects in PowerShell.
How do you add a property City with value Seattle to a custom object $obj using direct assignment?
C. The parameter '-Property' should be '-Name' when adding a property.
D. Write-Output cannot access properties added by Add-Member.
Solution
Step 1: Check Add-Member parameters
The parameter to specify the property name is -Name, not -Property. Using -Property causes an error.
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.
Final Answer:
The parameter '-Property' should be '-Name' when adding a property. -> Option C
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
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.
Step 2: Add property conditionally
If 'Year' does not exist, Add-Member adds it with value 2023. This avoids errors from duplicate properties.
Final Answer:
if (-not $car.PSObject.Properties.Name -contains 'Year') { Add-Member -InputObject $car -MemberType NoteProperty -Name Year -Value 2023 } -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.