📖 Scenario: You are managing a list of employees in your company. Each employee is represented as an object with some basic information. You want to add more details to each employee by adding new properties.
🎯 Goal: Learn how to add new properties to existing PowerShell objects to enrich their information.
📋 What You'll Learn
Create a list of employee objects with initial properties
Add a new property to each employee object
Use a variable to hold the new property value
Print the updated employee objects to see the new properties
💡 Why This Matters
🌍 Real World
Adding properties to objects is useful when you want to enrich data records with more details, like adding department info to employee records.
💼 Career
Many automation tasks require updating or enhancing objects with new information, which is common in system administration and data processing.
Progress0 / 4 steps
1
Create initial employee objects
Create a variable called employees that holds an array of two custom objects. The first object should have properties Name with value "Alice" and Age with value 30. The second object should have properties Name with value "Bob" and Age with value 25.
PowerShell
Hint
Use [PSCustomObject]@{} to create each employee object and put them inside an array @(...).
2
Create a new property value variable
Create a variable called department and set it to the string "Sales".
PowerShell
Hint
Just assign the string "Sales" to the variable $department.
3
Add the new property to each employee
Use a foreach loop with the variable employee to go through each object in $employees. Inside the loop, add a new property called Department to $employee and set it to the value of $department.
PowerShell
Hint
Use Add-Member with -MemberType NoteProperty to add the new property.
4
Print the updated employees
Use Write-Output to print the $employees variable so you can see the new Department property for each employee.
PowerShell
Hint
Just write Write-Output $employees to show the updated list.
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
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.
Step 2: Compare options
Options A, B, and C describe converting, deleting, or creating objects, which are not the purpose of Add-Member.
Final Answer:
It adds new properties or methods to an existing object. -> Option B
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
Step 1: Identify correct parameter names
The correct parameters for Add-Member are -InputObject, -MemberType, -Name, and -Value.
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.
Final Answer:
Add-Member -InputObject $person -MemberType NoteProperty -Name Age -Value 30 -> Option D
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.