📖 Scenario: You are managing a small inventory system for a local bookstore. You want to keep track of books using PowerShell objects. Each book has a Title, Author, and Price. You will create an array of book objects, set a price limit, filter books below that price, and display the filtered list.
🎯 Goal: Build a PowerShell script that creates an array of book objects, sets a price limit, filters books cheaper than the limit, and prints the filtered books.
📋 What You'll Learn
Create an array of book objects with exact properties and values
Add a variable for the price limit
Filter the array to include only books cheaper than the price limit
Print the filtered list of books
💡 Why This Matters
🌍 Real World
Managing collections of items like books, products, or users is common in automation scripts. Object arrays let you organize and filter data easily.
💼 Career
PowerShell scripting with object arrays is useful for IT admins and automation engineers to handle system inventories, reports, and data filtering.
Progress0 / 4 steps
1
Create an array of book objects
Create an array called $books containing these three book objects with properties Title, Author, and Price: 'The Hobbit', 'J.R.R. Tolkien', 15.99; '1984', 'George Orwell', 12.5; 'Pride and Prejudice', 'Jane Austen', 9.99.
PowerShell
Hint
Use [PSCustomObject] to create each book object and put them inside an array @( ... ).
2
Set a price limit variable
Create a variable called $priceLimit and set it to 13.
PowerShell
Hint
Just assign the number 13 to the variable $priceLimit.
3
Filter books cheaper than the price limit
Create a new variable called $affordableBooks that contains only the books from $books where the Price is less than $priceLimit. Use the Where-Object cmdlet with a script block comparing $_ object's Price property to $priceLimit.
PowerShell
Hint
Use Where-Object { $_.Price -lt $priceLimit } to filter the array.
4
Print the filtered list of affordable books
Print the $affordableBooks variable to display the filtered books.
PowerShell
Hint
Simply type $affordableBooks to print the filtered list.
Practice
(1/5)
1. What is an object array in PowerShell?
easy
A. A type of function that returns objects
B. A single object with many properties
C. A command to create new objects
D. A variable that holds multiple objects together in a list
Solution
Step 1: Understand the definition of object arrays
An object array is a collection that holds many objects in one variable, like a list.
Step 2: Compare options with the definition
Only A variable that holds multiple objects together in a list correctly describes an object array as a variable holding multiple objects.
Final Answer:
A variable that holds multiple objects together in a list -> Option D
Quick Check:
Object array = list of objects [OK]
Hint: Remember: arrays hold many items together [OK]
Common Mistakes:
Confusing object arrays with single objects
Thinking object arrays are commands
Mixing arrays with functions
2. Which of the following is the correct way to create an empty object array in PowerShell?
easy
A. $array = ()
B. $array = @()
C. $array = new-object array
D. $array = {}
Solution
Step 1: Recall PowerShell syntax for empty arrays
PowerShell uses @() to create an empty array.
Step 2: Check each option
$array = @() uses @(), which is correct. $array = () uses (), which assigns $null, not an array. $array = new-object array is incorrect syntax. $array = {} uses {}, which creates a hashtable, not an array.
Final Answer:
$array = @() -> Option B
Quick Check:
Empty array syntax = @() [OK]
Hint: Use @() to create empty arrays in PowerShell [OK]
Common Mistakes:
Using () which assigns null, not an array
Confusing arrays with hashtables {}
Trying to use new-object for arrays
3. What will be the output of this PowerShell code?