0
0
PowerShellscripting~5 mins

Adding properties to objects in PowerShell - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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 30
Click 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?
ASet-Property
BAdd-Member
CNew-Property
DAdd-Property
How do you add a property City with value Seattle to a custom object $obj using direct assignment?
A$obj | Add-Property City 'Seattle'
BAdd-Member -InputObject $obj -NotePropertyName 'City' -NotePropertyValue 'Seattle'
CSet-Property -Object $obj -Name City -Value 'Seattle'
D$obj.City = 'Seattle'
What happens if you try to add a property by direct assignment to an object that does not support dynamic properties?
AIt throws an error
BIt adds the property successfully
CIt silently ignores the assignment
DIt converts the object to a string
Which of the following creates a new object that supports adding properties dynamically?
ANew-Object PSObject -Property @{}
BNew-Object System.Object
CNew-Object String
DNew-Object Int32
What parameter do you use with Add-Member to specify the property name?
A-MemberName
B-PropertyName
C-NotePropertyName
D-Name
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.