0
0
PowerShellscripting~15 mins

Sort-Object for ordering in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Sort-Object for ordering
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use foreach ($product in $sortedProducts) and Write-Output with string interpolation.