3.9.1. Script Types and Basic Constructs
💡 First Principle: Different scripting languages exist because different environments have different native capabilities. PowerShell is the native Windows automation language; bash is the native Linux shell. Understanding which tool works in which environment is prerequisite to understanding when to use each.
Script Types
| Script Type | Environment | Strengths | File Extension |
|---|---|---|---|
| PowerShell | Windows (also cross-platform) | Deep Windows/AD integration, objects (not text), remoting | .ps1 |
| Batch | Windows (legacy) | Simple, runs on all Windows versions, no dependencies | .bat, .cmd |
| Bash | Linux/Unix/macOS | Text processing, system integration, widespread | .sh |
| VBScript (VBS) | Windows (legacy) | COM object automation; largely replaced by PowerShell | .vbs |
Environment Variables
Environment variables store configuration values accessible to scripts and processes without hardcoding them:
- Windows:
%SystemRoot%,%USERNAME%,%COMPUTERNAME% - Linux:
$HOME,$PATH,$USER
Basic Script Constructs
# PowerShell example — shows all three constructs
$servers = @("server01", "server02", "server03") # Array variable
foreach ($server in $servers) { # Loop
if (Test-Connection -ComputerName $server) { # Conditional
Write-Host "$server is online"
} else {
Write-Host "$server is OFFLINE" # Comparator result
}
}
| Construct | Purpose | PowerShell Example | Bash Example |
|---|---|---|---|
| Variables | Store values | $name = "value" | name="value" |
| Loops | Repeat actions | foreach ($x in $list) | for x in list |
| Conditionals | Branch logic | if ($x -eq 5) | if [ $x -eq 5 ] |
| Comparators | Compare values | -eq, -ne, -gt, -lt | =, !=, -gt, -lt |
Basic Data Types
- Integers: Whole numbers (
42,1024) - Strings: Text values (
"server01","C:\Logs") - Arrays: Collections of values (
@("server01", "server02")in PowerShell; space-separated in bash)
Comment Syntax
| Language | Comment Character | Example |
|---|---|---|
| PowerShell | # | # This is a comment |
| Bash | # | # This is a comment |
| Batch | REM or :: | REM This is a comment |
| VBScript | ' | ' This is a comment |
⚠️ Exam Trap: PowerShell uses -eq for equality comparison, not ==. Using == is a common mistake for developers coming from other languages. The exam tests basic construct syntax recognition.
Reflection Question: A script needs to check whether a list of servers is online and send an alert if any are offline. Which script construct is responsible for checking each server in the list, and which handles the "if offline, alert" logic?