📖 Scenario: You work in a small store and have a list of products with their prices. You want to find which products cost less than $20 to decide what to promote in a sale.
🎯 Goal: Build a PowerShell script that filters a list of products to show only those priced below $20 using Where-Object.
📋 What You'll Learn
Create a list of products with their prices as objects
Set a price limit variable to 20
Use Where-Object to filter products cheaper than the price limit
Display the filtered products
💡 Why This Matters
🌍 Real World
Filtering lists of items by conditions is common in managing inventories, logs, or user data.
💼 Career
Knowing how to filter data with PowerShell helps automate tasks in IT support, system administration, and data processing.
Progress0 / 4 steps
1
Create the product list
Create a variable called products that holds an array of three objects. Each object should have two properties: Name and Price. Use these exact entries: Name = 'Pen', Price = 5, Name = 'Notebook', Price = 15, Name = 'Backpack', Price = 45.
PowerShell
Hint
Use @( ... ) to create an array and @{Name=''; Price=} to create objects.
2
Set the price limit
Create a variable called priceLimit and set it to 20.
PowerShell
Hint
Just assign the number 20 to $priceLimit.
3
Filter products using Where-Object
Create a variable called cheapProducts that stores the result of filtering $products using Where-Object. Keep only products where Price is less than $priceLimit. Use the syntax: $products | Where-Object { $_.Price -lt $priceLimit }.
PowerShell
Hint
Use Where-Object with a script block that checks if Price is less than $priceLimit.
4
Display the filtered products
Use Write-Output to print the $cheapProducts variable.
PowerShell
Hint
Just write Write-Output $cheapProducts to show the filtered list.
Practice
(1/5)
1. What does the Where-Object cmdlet do in PowerShell?
easy
A. Filters objects in a collection based on a condition
B. Sorts objects alphabetically
C. Creates new objects from existing ones
D. Deletes objects from a collection
Solution
Step 1: Understand the purpose of Where-Object
Where-Object is used to filter items in a collection by applying a condition to each item.
Step 2: Compare options with the purpose
Only Filters objects in a collection based on a condition correctly describes filtering based on a condition. Sorting, creating, or deleting are different operations.
Final Answer:
Filters objects in a collection based on a condition -> Option A
Quick Check:
Where-Object filters collections = A [OK]
Hint: Where-Object selects items matching a condition [OK]
Common Mistakes:
Confusing filtering with sorting
Thinking it creates or deletes objects
Ignoring the condition part
2. Which of the following is the correct syntax to filter processes with CPU usage greater than 100 using Where-Object?
easy
A. Get-Process | Where-Object { CPU > 100 }
B. Get-Process | Where-Object { $_.CPU -gt 100 }
C. Get-Process | Where-Object CPU -gt 100
D. Get-Process | Where-Object CPU > 100
Solution
Step 1: Identify correct use of script block and $_ variable
Where-Object requires a script block with curly braces and uses $_ to refer to the current object.
Step 2: Check each option's syntax
Get-Process | Where-Object { $_.CPU -gt 100 } correctly uses { $_.CPU -gt 100 }. Versions without the script block braces {} are incorrect. The version with { CPU > 100 } misses $_ before CPU.