0
0
PowerShellscripting~30 mins

Comparison operators (-eq, -ne, -gt, -lt) in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Comparison Operators in PowerShell
📖 Scenario: You are helping a store manager check product stock levels to decide if they need to reorder items.
🎯 Goal: Build a PowerShell script that uses comparison operators -eq, -ne, -gt, and -lt to compare stock counts and print messages.
📋 What You'll Learn
Create a dictionary of products with their stock counts
Add a reorder threshold variable
Use comparison operators to check stock levels
Print messages based on the comparisons
💡 Why This Matters
🌍 Real World
Store managers often need to check stock levels to decide when to reorder products. Automating this saves time and avoids running out of items.
💼 Career
Knowing how to use comparison operators in scripts helps automate routine checks and decisions in IT, operations, and inventory management roles.
Progress0 / 4 steps
1
Create the product stock dictionary
Create a dictionary called products with these exact entries: 'Apples' = 50, 'Bananas' = 20, 'Oranges' = 0, 'Grapes' = 15.
PowerShell
Need a hint?

Use @{} to create a dictionary in PowerShell with keys and values separated by =.

2
Add a reorder threshold variable
Create a variable called reorderThreshold and set it to 10.
PowerShell
Need a hint?

Just assign the number 10 to the variable $reorderThreshold.

3
Check stock levels using comparison operators
Use a foreach loop with variables product and stock to iterate over $products.GetEnumerator(). Inside the loop, use -lt to check if stock is less than $reorderThreshold and store the result in a variable called needsReorder.
PowerShell
Need a hint?

Use $products.GetEnumerator() to loop through the dictionary. Use -lt to compare stock with threshold.

4
Print messages based on stock comparison
Inside the foreach loop, use if with $needsReorder to print "Reorder needed for $product" if true, else print "Stock is sufficient for $product".
PowerShell
Need a hint?

Use if ($needsReorder) to decide which message to print with Write-Output.