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
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
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
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
Hint
Use Write-Output $filteredEmployees to show the filtered list.
Practice
(1/5)
1. What is the main purpose of using [PSCustomObject] in PowerShell?
easy
A. To convert strings into numbers
B. To create objects with named properties for organizing related data
C. To create arrays of numbers automatically
D. To run commands faster in PowerShell
Solution
Step 1: Understand what [PSCustomObject] does
[PSCustomObject] creates an object with named properties to hold related data clearly.
Step 2: Compare options
Only "To create objects with named properties for organizing related data" correctly describes this purpose. Other options describe unrelated tasks.
Final Answer:
To create objects with named properties for organizing related data -> Option B
Quick Check:
Purpose of [PSCustomObject] = Organize data [OK]
Hint: Remember: [PSCustomObject] groups data with names [OK]
Common Mistakes:
Thinking it speeds up command execution
Confusing with arrays or type conversion
Assuming it creates numeric sequences
2. Which of the following is the correct syntax to create a custom object with properties Name and Age in PowerShell?
easy
A. [PSCustomObject] @{Name: 'Alice'; Age: 30}
B. @{Name='Alice', Age=30} [PSCustomObject]
C. [PSCustomObject]@{Name='Alice'; Age=30}
D. [PSCustomObject]@{Name='Alice' Age=30}
Solution
Step 1: Recall correct syntax for custom object creation
The correct syntax uses [PSCustomObject]@{PropertyName='Value'; PropertyName2=Value2} with semicolons separating properties.
Step 2: Analyze each option
[PSCustomObject]@{Name='Alice'; Age=30} matches the correct syntax. @{Name='Alice', Age=30} [PSCustomObject] reverses order. [PSCustomObject] @{Name: 'Alice'; Age: 30} uses colons instead of equals. [PSCustomObject]@{Name='Alice' Age=30} misses semicolon between properties.
Final Answer:
[PSCustomObject]@{Name='Alice'; Age=30} -> Option C
Quick Check:
Correct syntax uses semicolons and equals [OK]
Hint: Use semicolons and equals inside @{ } with [PSCustomObject] [OK]
Common Mistakes:
Using commas instead of semicolons
Placing [PSCustomObject] after the hashtable
Using colons instead of equals signs
3. What will be the output of this PowerShell code?
Step 1: Understand object creation and property access
The variable $person holds a custom object with properties Name and Age. Accessing $person.Name returns the value of the Name property.
Step 2: Determine the output
The Name property was set to 'Bob', so the output is 'Bob'.
Final Answer:
Bob -> Option A
Quick Check:
Access property returns its value [OK]
Hint: Access properties with dot notation to get their values [OK]
Common Mistakes:
Expecting the whole object printed
Confusing property name with value
Using wrong property name
4. Identify the error in this PowerShell code:
$obj = [PSCustomObject]@{Name='Eve' Age=28}
medium
A. Missing semicolon between properties inside the hashtable
B. Incorrect use of square brackets around PSCustomObject
C. Property names must be in double quotes
D. Cannot assign numbers as property values
Solution
Step 1: Check hashtable property separators
Properties inside the hashtable must be separated by semicolons. Here, Name='Eve' and Age=28 are missing a semicolon between them.
Step 2: Verify other syntax elements
Square brackets around PSCustomObject are correct. Property names do not require quotes. Numbers can be property values.
Final Answer:
Missing semicolon between properties inside the hashtable -> Option A
Quick Check:
Separate properties with semicolons [OK]
Hint: Separate each property with a semicolon inside @{ } [OK]
Common Mistakes:
Using commas instead of semicolons
Adding quotes around property names unnecessarily
Thinking numbers can't be property values
5. You want to create a list of custom objects representing books with properties Title and Author. Which PowerShell code correctly creates an array of two such objects?
hard
A. [PSCustomObject]@{Title='1984', Author='Orwell'; Title='Dune', Author='Herbert'}
B. [PSCustomObject]@{Title='1984', Author='Orwell'}, [PSCustomObject]@{Title='Dune'; Author='Herbert'}
C. @{Title='1984'; Author='Orwell'}, @{Title='Dune'; Author='Herbert'}
D. @([PSCustomObject]@{Title='1984'; Author='Orwell'}, [PSCustomObject]@{Title='Dune'; Author='Herbert'})
Solution
Step 1: Understand how to create multiple custom objects in an array
Each book must be a separate [PSCustomObject]. To create an array, wrap the objects in @( ) and separate them with commas.
Step 2: Analyze each option
@([PSCustomObject]@{Title='1984'; Author='Orwell'}, [PSCustomObject]@{Title='Dune'; Author='Herbert'}) correctly creates two separate custom objects inside an array. [PSCustomObject]@{Title='1984', Author='Orwell'}, [PSCustomObject]@{Title='Dune'; Author='Herbert'} uses comma instead of semicolon in the first hashtable, causing a syntax error. @{Title='1984'; Author='Orwell'}, @{Title='Dune'; Author='Herbert'} creates hashtables, not custom objects. [PSCustomObject]@{Title='1984', Author='Orwell'; Title='Dune', Author='Herbert'} tries to combine two books in one object incorrectly.
Final Answer:
@([PSCustomObject]@{Title='1984'; Author='Orwell'}, [PSCustomObject]@{Title='Dune'; Author='Herbert'}) -> Option D
Quick Check:
Use array @( ) with separate custom objects [OK]
Hint: Wrap multiple [PSCustomObject]s in @( ) separated by commas [OK]
Common Mistakes:
Using commas instead of semicolons inside hashtables
Using hashtables instead of custom objects
Combining multiple items in one object incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.