Variables and Data Types
Store strings, numbers, arrays, and hashtables in PowerShell variables.
What You'll Learn
- Creating and using variables with the $ prefix
- String interpolation (double vs single quotes)
- Arrays, hashtables, and ordered hashtables
- Automatic variables: $_, $null, $HOME, $Error
Variables and Types
In PowerShell, variables start with $ and are dynamically typed — you don't declare a type, PowerShell figures it out. Under the hood, every variable is a .NET object.
$name = "Alice" # System.String $age = 30 # System.Int32 $price = 19.99 # System.Double $isAdmin = $true # System.Boolean $nothing = $null # Null
String interpolation is one of the most important features. Double-quoted strings expand variables; single-quoted strings are literals:
"Hello, $name!" → Hello, Alice! 'Hello, $name!' → Hello, $name! "2 + 2 = $(2 + 2)" → 2 + 2 = 4 (subexpression)
⚠️ Common Mistake
Using single quotes when you want interpolation: 'User: $name' prints the literal text $name. Always use double quotes for variable expansion.
Variables & Types
Explore PowerShell variables, types, and string interpolation.
// PowerShell Variables — simulated in JavaScript
console.log("=== Variables in PowerShell ===");
console.log();
console.log("Variables start with $ and are dynamically typed:");
console.log();
// Simulate PS variables
let name = "Alice";
let age = 30;
let isAdmin = true;
let scores = [95, 87, 92, 88];
console.log(' $name = "Alice" → String: ' + name);
console.log(" $age = 30 → Int32: " + age);
console.log(" $isAdmin = $true → Boolean: " + isAdmin);
console.log(" $sc
...Collections: Arrays & Hashtables
Arrays store multiple values. Create them with @() syntax:
$colors = @("Red", "Green", "Blue")
$colors[0] # Red
$colors[-1] # Blue (last)
$colors += "Yellow" # Add item (creates new array!)Hashtables are key-value pairs (like dictionaries). Use @{}:
$server = @{
Name = "WebServer01"
IP = "192.168.1.10"
Status = "Running"
}
$server.Name # WebServer01
$server["IP"] # 192.168.1.10
$server.Port = 8080 # Add new keyUse [ordered]@{} when key order matters — regular hashtables don't guarantee order.
Collections
Work with arrays, hashtables, and automatic variables.
// Arrays & Hashtables — simulated in JavaScript
console.log("=== Arrays ===");
console.log();
let fruits = ["Apple", "Banana", "Cherry"];
console.log(" $fruits = @('Apple', 'Banana', 'Cherry')");
console.log(" $fruits[0] → " + fruits[0]);
console.log(" $fruits[-1] → " + fruits[fruits.length - 1] + " (last item)");
console.log(" $fruits.Count → " + fruits.length);
console.log();
// Adding to array
fruits.push("Date");
console.log(" $fruits += 'Date' → [" + fruits.join(", ") +
...💡 Pro Tip
For large collections that grow frequently, use [System.Collections.ArrayList] instead of @(). The += operator on arrays creates a brand-new array each time, which is O(n) — ArrayList's .Add() is O(1).
📋 Quick Reference
| Type | Syntax | Example |
|---|---|---|
| String | "text" | $name = "Alice" |
| Integer | 42 | $count = 42 |
| Boolean | $true/$false | $ok = $true |
| Array | @() | $arr = @(1,2,3) |
| Hashtable | @{} | $h = @{A=1} |
🎉 Lesson Complete!
You can now work with variables, strings, arrays, and hashtables. Next up: understanding PowerShell's powerful object pipeline.
Sign up for free to track which lessons you've completed and get learning reminders.