Lesson 2 of 5  ยท  Beginner

Variables & Data Types

โฑ ~15 minutes Beginner

What is a Variable?

A variable is like a named container that holds a piece of data. You can store a value in it and refer to it later by name.

In PowerShell, all variables start with a dollar sign $. This makes them easy to spot.

Your First Variables
# Assign a value to a variable
$name = "Alice"
$age = 30

# Read the variable
PS C:\> $name
Alice

# Use it in a message (string interpolation)
PS C:\> Write-Host "Hello, $name! You are $age years old."
Hello, Alice! You are 30 years old.
๐Ÿ’ก
String interpolation: Variables inside double quotes ("...") are automatically replaced with their values. Variables inside single quotes ('...') are treated as literal text โ€” no substitution happens.

Data Types

PowerShell is loosely typed โ€” it figures out the type automatically. But it's good to understand what types exist:

Common Data Types
# String (text)
$greeting = "Hello, World!"

# Integer (whole number)
$count = 42

# Double (decimal number)
$price = 9.99

# Boolean (true or false)
$isActive = $true
$isDeleted = $false

# Null (nothing / empty)
$nothing = $null

# Check the type of a variable
$greeting.GetType().Name
String

String Operations

Strings have many useful built-in methods. Since everything in PowerShell is an object, you call methods with a dot:

String Methods
$text = " Hello, PowerShell! "

# Remove leading/trailing spaces
$text.Trim()
Hello, PowerShell!

# Convert to uppercase / lowercase
$text.ToUpper()
HELLO, POWERSHELL!

# Replace text
$text.Replace("Hello", "Hi")
Hi, PowerShell!

# Check if it contains something
$text.Contains("PowerShell")
True

# Length of the string
$text.Length
22

# Split into an array
"one,two,three".Split(",")
one
two
three

Numbers and Math

Math Operations
$a = 10
$b = 3

$a + $b # 13 (addition)
$a - $b # 7 (subtraction)
$a * $b # 30 (multiplication)
$a / $b # 3.33 (division)
$a % $b # 1 (modulo โ€“ remainder)
$a ** $b # 1000 (power, PS 7+)

# Math library
[math]::Round(3.7) # 4
[math]::Abs(-15) # 15
[math]::Sqrt(16) # 4
[math]::Max(5, 12) # 12

Arrays

An array holds multiple values in a single variable. Think of it as a numbered list.

Arrays
# Create an array
$fruits = "Apple", "Banana", "Cherry"
# OR using @()
$numbers = @(1, 2, 3, 4, 5)

# Access by index (starts at 0)
$fruits[0] # Apple
$fruits[1] # Banana
$fruits[-1] # Cherry (last item)

# Number of items
$fruits.Count # 3

# Add an item
$fruits += "Mango"

# Range shortcut
$range = 1..10 # [1,2,3,4,5,6,7,8,9,10]

Hashtables

A hashtable stores key-value pairs โ€” like a dictionary or a lookup table. Keys are unique and you use them to retrieve values.

Hashtables
# Create a hashtable with @{ key = value }
$person = @{
    Name = "Alice"
    Age = 30
    Country = "Sweden"
}

# Access values (two ways)
$person.Name # Alice
$person["Age"] # 30

# Add or update a key
$person.Email = "alice@example.com"

# Remove a key
$person.Remove("Country")

# List all keys
$person.Keys
Name
Age
Email

Type Conversion

Sometimes you need to convert between types:

Type Conversion
# String to number
[int]"42" # 42
[double]"3.14" # 3.14

# Number to string
[string]100 # "100"
100.ToString() # "100"

# Check if something is a number
$str = "123"
$num = 0
[int]::TryParse($str, [ref]$num) # $true, $num=123

String Formatting with -f

The -f format operator lets you insert values into a string template cleanly:

String Formatting
$name = "Bob"
$score = 95.7

# {0} = first value, {1} = second, etc.
"Player {0} scored {1:F1}%" -f $name, $score
Player Bob scored 95.7%

# Format a number with commas and 2 decimal places
"{0:N2}" -f 1234567.891
1,234,567.89

๐Ÿงช Try It Yourself

  1. Create a variable $myName with your name and display it
  2. Create an array of 5 of your favorite things
  3. Create a hashtable describing yourself (name, age, city)
  4. Use string interpolation to build a sentence using your variables
  5. Try "hello".ToUpper() and " spaces ".Trim()

Key Takeaways

  • Variables always start with $
  • Use = to assign, just refer to the name to read
  • Double quotes allow variable interpolation; single quotes are literal
  • Arrays use @() or comma-separated values; index starts at 0
  • Hashtables use @{} with key = value pairs
  • Everything in PowerShell is an object with properties and methods