When you write PowerShell commands, you often need to reuse the same folder path, username, or number multiple times. Typing it over and over is tiring and error-prone. Instead, you can store it once in a PowerShell Variable.

The Labeled Storage Box Analogy

Think of a variable like a labeled cardboard box on your desk. You write a name on the outside of the box (for example, $name) and put a value inside it. Whenever you need that value later, you just ask PowerShell for the box by its label.

Creating and Using Your First Variable

In PowerShell, every variable name starts with a dollar sign ($). You create one using the = sign:

# Store text (a string) and a number in variables
$name = "Alex"
$age = 25

# Print a friendly sentence using both variables
Write-Host "Hello, $name! You are $age years old."

Notice how PowerShell automatically replaces $name and $age inside double quotes ("...") with their stored values.

Common Types of Data You Can Store

You do not have to tell PowerShell whether a value is text or a number—it figures that out automatically:

$city = "Jakarta"     # String (text)
$items = 10           # Integer (whole number)
$isReady = $true      # Boolean ($true or $false)

Storing a List of Items (Arrays)

Need to store several items in one box, like a shopping list? Wrap them in @(...) to create an array. PowerShell starts counting list positions at 0:

# Create a list of fruits
$fruits = @("apple", "banana", "cherry")

# Get the first item (position 0)
Write-Host $fruits[0]   # Outputs: apple

3 Things to Remember

  • Start with $: All PowerShell variable names begin with a dollar sign, like $name.
  • Double quotes expand: Variables inside "..." automatically show their values in Write-Host.
  • Lists start at 0: Use @("a", "b") to store multiple items and access the first one with [0].