PowerShell Lesson 4: Variables, Loops and Conditions

📎 This article includes 2 downloadable practice files ↓

⏱ 1 min read

📘 PowerShell Beginner Course · Lesson 4 of 6

“`powershell
$rate = 0.18
$amounts = 1000, 25000, 120000
foreach ($a in $amounts) {
$band = if ($a -ge 100000) { “Large” } elseif ($a -ge 10000) { “Medium” } else { “Small” }
“{0,8} GST {1,8:N2} {2}” -f $a, ($a * $rate), $band
}
$prices = @{ Laptop = 42000; Router = 3400 } # hashtable (dictionary)
$prices[“Laptop”]
“`

  • Variables start with $; arrays with commas or @( ); hashtables with @{ }.
  • -f formats: {1,8:N2} = argument 1, width 8, number with 2 decimals.
💡 Strings in double quotes expand variables (“Total $total”); single quotes don’t (‘Total $total’). Use single quotes for literal text.

Practice

Run the script and add a fourth amount.

📎 Practice files for this article

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong

Leave a Reply

Your email address will not be published. Required fields are marked *