Recall & Review
beginner
What is the simplest way to add a new property to an existing PowerShell object?
Use the <code>Add-Member</code> cmdlet with the <code>-NotePropertyName</code> and <code>-NotePropertyValue</code> parameters to add a new property.Click to reveal answer
beginner
How do you add a property named
Age with value 30 to a PowerShell object stored in variable $person?Use:
Add-Member -InputObject $person -NotePropertyName 'Age' -NotePropertyValue 30Click to reveal answer
intermediate
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?
✗ 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?✗ Incorrect
Direct assignment like
$obj.City = 'Seattle' adds the property if the object supports it.What happens if you try to add a property by direct assignment to an object that does not support dynamic properties?
✗ Incorrect
Direct assignment fails with an error if the object does not support adding new properties dynamically.
Which of the following creates a new object that supports adding properties dynamically?
✗ Incorrect
PSObject supports dynamic properties, unlike basic system types.What parameter do you use with
Add-Member to specify the property name?✗ Incorrect
The parameter
-NotePropertyName specifies the name of the new property.Explain two ways to add a new property to an existing PowerShell object and when you might use each.
Think about object types and how flexible they are.
You got /4 concepts.
Describe how to create a new PowerShell object that allows adding properties easily and how to add a property to it.
Start with creating the object, then add properties.
You got /3 concepts.