
📎 This article includes 2 downloadable practice files ↓
“`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@{ }. -fformats:{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
- 🖥️ps-04-variables-loops.ps1Lesson code file.⬇ PS1 · 323 B
- 📄ps-04-variables-loops-output.txtOutput from our test run.⬇ TXT · 103 B
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