0
0
PowerShellscripting~15 mins

Custom objects (PSCustomObject) in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Create and Use Custom Objects (PSCustomObject) in PowerShell
📖 Scenario: You work in a small company where you need to organize employee information clearly. You want to create a simple list of employees with their names and ages using PowerShell custom objects.
🎯 Goal: Build a PowerShell script that creates custom objects for employees, adds a filter condition, and then displays the filtered list.
📋 What You'll Learn
Create a list of custom objects with exact employee names and ages
Add a variable to set the minimum age for filtering
Use a loop or pipeline to filter employees older than the minimum age
Print the filtered list of employees
💡 Why This Matters
🌍 Real World
Organizing and filtering structured data like employee records is common in IT and business automation tasks.
💼 Career
Knowing how to create and manipulate custom objects in PowerShell is essential for system administrators and automation engineers to handle complex data easily.
Progress0 / 4 steps
1
Create a list of employee custom objects
Create a variable called employees that holds an array of three custom objects. Each object must have two properties: Name and Age. Use these exact entries: Name = 'Alice', Age = 28, Name = 'Bob', Age = 34, and Name = 'Charlie', Age = 22. Use [PSCustomObject] to create each object.
PowerShell
Need a hint?

Use @( ... ) to create an array and [PSCustomObject]@{} to create each object with properties.

2
Set a minimum age filter
Create a variable called minAge and set it to 25. This will be used to filter employees older than this age.
PowerShell
Need a hint?

Just create a simple variable $minAge and assign the number 25.

3
Filter employees older than the minimum age
Create a variable called filteredEmployees that contains only the employees from $employees whose Age is greater than $minAge. Use the Where-Object cmdlet with a script block that compares $_ .Age to $minAge.
PowerShell
Need a hint?

Use the pipeline | and Where-Object { $_.Age -gt $minAge } to filter.

4
Display the filtered employees
Use Write-Output to print the $filteredEmployees variable so you can see the filtered list of employees.
PowerShell
Need a hint?

Use Write-Output $filteredEmployees to show the filtered list.