BeginnerLesson 3 of 16

Variables, Data Types, and Operators

Declare and use variables correctly, understand how PowerShell handles data types implicitly and explicitly, work with strings, arrays, and hashtables, and apply comparison and logical operators in conditions and filters.

🧒 Simple Explanation (ELI5)

A variable is a named box that holds a value. PowerShell variables start with $: $name = "Alice". The box can hold text, numbers, lists, or complex objects. Operators are the comparison words: equal, greater than, less than. They let you ask questions like "is this server's disk usage above 80%?" inside conditions and filters.

🔧 Why Do We Need It?

⚙️ Technical Explanation

PowerShell variables are declared with the $ sigil. Types are inferred by default but can be enforced with a cast: [int]$port = 8080. Common types: [string], [int], [bool], [datetime], [array], [hashtable], [PSCustomObject].

PowerShell strings support two quote styles: double quotes allow variable expansion ("Hello $name") and escape sequences (`n = newline, `t = tab). Single quotes treat everything literally ('Hello $name' outputs the literal text $name).

Comparison operators use letter-based syntax (not symbols, except for some assignments): -eq, -ne, -gt, -lt, -ge, -le. For case-sensitive versions, prefix with c: -ceq, -cne. String matching: -like (wildcards *), -match (regex). Logical operators: -and, -or, -not, !.

🔤
Single vs Double Quotes Matter

Double quotes expand variables: "Server is $serverName" outputs the server name. Single quotes are literal: 'Server is $serverName' outputs the text $serverName. A common script bug is using single quotes when you intended variable interpolation—double-check quote style whenever a variable does not expand as expected.

📋
Hashtables as Configuration Maps

Hashtables are the PowerShell equivalent of a dictionary. They are ideal for mapping server names to IPs, environment names to resource groups, or config keys to values. Define once: $envMap = @{ dev = 'rg-dev'; staging = 'rg-staging'; prod = 'rg-prod' }. Access anywhere: $envMap[$env]. This pattern eliminates long if/elseif chains in deployment scripts.

📊 Visual Representation

Variable Types in Practice
$name = "web01"
[string]
$port = 8080
[int]
$servers = @("web01","web02")
[array]
$config = @{env="prod"}
[hashtable]
$isOnline = $true
[bool]

⌨️ Commands / Syntax

powershell
# Declare variables
$serverName = "web01"
[int]$port = 8080
[bool]$isProduction = $true

# String interpolation (double quotes expand variables)
Write-Host "Connecting to $serverName on port $port"

# String methods
$serverName.ToUpper()
$serverName.StartsWith("web")
$serverName.Replace("web", "db")

# Here-string for multi-line text
$body = @"
Server: $serverName
Port: $port
Environment: Production
"@

# Arrays
$servers = @("web01", "web02", "web03")
$servers.Count          # 3
$servers[0]             # "web01"
$servers += "web04"     # append

# Hashtables
$config = @{
    Environment = "prod"
    Region      = "uksouth"
    MinReplicas = 2
}
$config["Region"]       # "uksouth"
$config.Environment     # "prod"

# Comparison operators
$port -eq 8080          # True
$serverName -like "web*"  # True
$serverName -match "^\w+\d+$"  # True (regex)
5 -gt 3                 # True

# Logical operators
($port -eq 8080) -and $isProduction   # True
($port -eq 9090) -or ($port -eq 8080) # True
-not $isProduction                     # False

# Get variable type
$port.GetType().Name   # Int32

💼 Example (Real-world Use Case)

A deployment script uses a hashtable to map environment names to Azure resource groups, avoiding repeated if/elseif blocks: the caller passes -Environment prod and the script resolves $resourceGroup = $envMap[$Environment] in one line. This pattern scales to dozens of environments and prevents typos in hardcoded resource group names.

🧪 Hands-on

  1. Create a string variable for a server name and interpolate it: $srv = "web01"; Write-Host "Server: $srv".
  2. Build an array of three server names and get the count: $list = @("a","b","c"); $list.Count.
  3. Create a hashtable mapping two environment names to resource groups and retrieve one entry.
  4. Test the -like operator: "web01" -like "web*" should return True.
  5. Use Get-Date and store it in a variable: $now = Get-Date; $now.ToString("yyyy-MM-dd").
🎮
Try It Yourself

Build a configuration hashtable for a deployment script that has keys: Environment, ResourceGroup, Location, and MinInstances. Then write a string using those values: "Deploying to $($config.Environment) in $($config.Location)". Note the $() sub-expression syntax needed when accessing hashtable properties inside a double-quoted string.

🐛 Debugging Scenario

Problem: your script does a comparison $count -gt 10 but it is always $false even when $count should be 15.

🎯 Interview Questions

Beginner

How do you declare a variable in PowerShell?

Use the $ sigil: $name = "value". PowerShell infers the type automatically. To enforce a type, cast it: [int]$count = 5.

What is the difference between single and double quotes?

Double quotes expand variables and escape sequences. Single quotes treat content as a literal string. "Hello $name" outputs the value of $name; 'Hello $name' outputs the text "$name".

How do you create an array in PowerShell?

Use @(): $servers = @("web01", "web02"). Access elements with zero-based index: $servers[0]. Get the count with $servers.Count.

What are the comparison operators in PowerShell?

-eq (equal), -ne (not equal), -gt (greater than), -lt (less than), -ge (greater or equal), -le (less or equal), -like (wildcard match), -match (regex match). These use alphabetic names, not symbols, to avoid ambiguity.

What is a hashtable and when do you use one?

A hashtable is a key-value dictionary: $h = @{ key = "value" }. Use it for configuration maps, lookup tables, and named parameters when passing multiple values to functions or commands.

Intermediate

What causes silent type conversion bugs in PowerShell?

Values from CSV files, REST APIs, environment variables, and string splits are strings by default. If you compare them with -gt, -lt, or perform arithmetic, the comparison may work incorrectly or throw a type error. Always check GetType() or cast explicitly when the source is external data.

What is a here-string and when is it useful?

A here-string is a multi-line string literal delimited by @" ... "@. It preserves formatting and supports variable expansion. It is useful for embedding JSON payloads, HTML bodies, SQL queries, or configuration file content directly in scripts without string concatenation.

How do you access a property inside a string interpolation?

Use the sub-expression operator $(): "Region: $($config.Region)". Without $(), PowerShell would try to expand $config followed by a literal .Region and produce wrong output.

Scenario-based

A script reads server names from a CSV and compares ports, but comparisons return wrong results. How do you diagnose?

Run $row.Port.GetType().Name after importing the CSV. Import-Csv returns all columns as strings. Cast to the intended type: [int]$port = $row.Port, then compare. Alternatively, use ConvertFrom-Csv with explicit type conversion logic.

You need a deployment script that targets different Azure resource groups based on an environment parameter. How do you avoid a long if/elseif chain?

Use a hashtable lookup: $rgMap = @{dev='rg-dev'; staging='rg-staging'; prod='rg-prod'}; $rg = $rgMap[$Environment]. Add a validation check: if (-not $rg) { throw "Unknown environment: $Environment" }. This is clean, extensible, and self-documenting.

🌐 Real-world Usage

Azure deployment scripts use hashtables to map environment names to subscription IDs, resource groups, and locations. CI/CD pipeline scripts use typed variables to ensure port numbers, replica counts, and timeout values are integers before they are passed to commands. Array variables hold server lists that drive parallel Invoke-Command loops across fleets.

📝 Summary

Variables in PowerShell use the $ sigil and are dynamically typed by default. Enforce types with casts. Double quotes expand variables; single quotes are literal. Arrays hold ordered lists; hashtables hold key-value pairs. Comparison operators use alphabetic names (-eq, -gt, -like, -match). Type bugs from external data sources are the most common variable-related failure—always validate types when data comes from CSV, API, or environment variables.