📖 Scenario: You work in a small store and have a list of products with their prices. You want to organize this list so that the cheapest products come first.
🎯 Goal: Create a PowerShell script that sorts a list of products by their price in ascending order using Sort-Object.
📋 What You'll Learn
Create a list of products with their prices as objects
Add a variable to hold the sorting property
Use Sort-Object to sort the products by price
Print the sorted list showing product names and prices
💡 Why This Matters
🌍 Real World
Sorting lists of items by price or name is common in stores, inventory systems, and reports.
💼 Career
Knowing how to sort data in scripts helps automate tasks and prepare data for presentations or further processing.
Progress0 / 4 steps
1
Create the product list
Create a variable called products that contains three objects with these exact properties and values: Name as 'Apple', 'Banana', and 'Cherry'; Price as 1.20, 0.50, and 2.00 respectively.
PowerShell
Hint
Use [PSCustomObject] to create objects with Name and Price properties inside an array.
2
Add the sorting property variable
Create a variable called sortProperty and set it to the string 'Price' to specify the property to sort by.
PowerShell
Hint
Just assign the string 'Price' to the variable sortProperty.
3
Sort the products by price
Create a variable called sortedProducts that stores the result of sorting products by the property stored in sortProperty using Sort-Object.
PowerShell
Hint
Use the pipeline | and Sort-Object -Property $sortProperty to sort.
4
Print the sorted products
Use foreach to loop over sortedProducts and print each product's Name and Price in the format: Product: Apple, Price: 1.2.
PowerShell
Hint
Use foreach ($product in $sortedProducts) and Write-Output with string interpolation.
Practice
(1/5)
1. What does the PowerShell cmdlet Sort-Object do?
easy
A. It creates new objects from scratch.
B. It arranges objects in a specified order based on property values.
C. It deletes objects from a list.
D. It counts the number of objects in a list.
Solution
Step 1: Understand the purpose of Sort-Object
Sort-Object is used to order or arrange objects based on their properties or values.
Step 2: Compare options with the cmdlet's function
Only It arranges objects in a specified order based on property values. correctly describes arranging objects in order. Other options describe unrelated actions.
Final Answer:
It arranges objects in a specified order based on property values. -> Option B
Quick Check:
Sort-Object orders data = C [OK]
Hint: Sort-Object means arranging data in order [OK]
Common Mistakes:
Confusing sorting with deleting or counting
Thinking Sort-Object creates new objects
Assuming it only works on numbers
2. Which of the following is the correct syntax to sort a list of numbers stored in $numbers in descending order?
easy
A. $numbers | Sort-Object Descending
B. $numbers | Sort-Object /Descending
C. $numbers | Sort-Object -descend
D. $numbers | Sort-Object -Descending
Solution
Step 1: Recall the correct parameter for descending order
The correct parameter is -Descending with a dash and full word.
Step 2: Check each option's syntax
$numbers | Sort-Object -Descending uses the correct dash and spelling. Options A, C, and D have syntax errors or wrong parameter format.
Final Answer:
$numbers | Sort-Object -Descending -> Option D
Quick Check:
Use dash and full '-Descending' [OK]
Hint: Use dash before parameters like -Descending [OK]
You want to sort by Score descending, but if scores are equal, sort by Name ascending. Which command achieves this?
hard
A. $people | Sort-Object -Property @{Expression='Score';Descending=$true}, @{Expression='Name';Descending=$false}
B. $people | Sort-Object Name, Score -Descending
C. $people | Sort-Object Score -Descending | Sort-Object Name
D. $people | Sort-Object -Property Score, Name -Descending
Solution
Step 1: Understand sorting by multiple properties with different orders
You want Score sorted descending and Name sorted ascending when scores tie.
Step 2: Analyze each option's approach
$people | Sort-Object -Property @{Expression='Score';Descending=$true}, @{Expression='Name';Descending=$false} uses script blocks to specify descending for Score and ascending for Name correctly. $people | Sort-Object -Property Score, Name -Descending applies descending to both, which is wrong. $people | Sort-Object Score -Descending | Sort-Object Name sorts twice, losing the first order. $people | Sort-Object Name, Score -Descending sorts Name first, which is incorrect order.
Final Answer:
$people | Sort-Object -Property @{Expression='Score';Descending=$true}, @{Expression='Name';Descending=$false} -> Option A
Quick Check:
Use script blocks for mixed ascending/descending [OK]
Hint: Use script blocks to mix ascending and descending [OK]
Common Mistakes:
Applying -Descending to all properties at once
Sorting twice losing original order
Sorting properties in wrong priority order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.