0
0
PowerShellscripting~15 mins

Adding properties to objects in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Adding properties to objects
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Just write Write-Output $employees to show the updated list.