PowerShell Variables: How to Create and Use Them

Think of a variable as a labeled container: you put something in it, give the container a name, and retrieve the contents whenever you need them. PowerShell variables work exactly that way — prefix any name with $, assign a value with =, and you’ve got a named container that holds anything from a simple number to a complex object. This tutorial covers everything you need to declare, assign, and use variables effectively in real scripts.
Declaring a Variable with $
In PowerShell, you don’t declare a type explicitly — you just assign a value and PowerShell infers the type. The $ prefix is mandatory; without it, PowerShell interprets the name as a command or string.
# Assign a string
$serverName = 'WebServer01'
# Assign a number
$port = 8080
# Assign the output of a command to a variable
$files = Get-ChildItem C:\Logs
# Display a variable's value
$serverName
$port
WebServer01
8080
Variable names are case-insensitive — $Server and $server refer to the same variable. By convention, use camelCase or PascalCase for readability.
String Variables and Quotes
PowerShell has two types of string delimiters that behave differently:
- Single quotes (
'...') — literal strings. No variable expansion, no escape sequences processed. - Double quotes (
"...") — expandable strings. Variables and subexpressions are interpolated.
$name = 'Alice'
# Double quotes: $name expands to its value
"Hello, $name"
# Single quotes: $name is treated as literal text
'Hello, $name'
Hello, Alice
Hello, $name
Use single quotes whenever you don’t need expansion — it’s clearer and avoids accidental substitutions. Switch to double quotes only when you need a variable’s value embedded in the string.
Numeric Variables and Math
PowerShell handles arithmetic naturally on numeric variables:
$a = 10
$b = 3
$a + $b # 13
$a - $b # 7
$a * $b # 30
$a / $b # 3.33333...
$a % $b # 1 (modulo)
$a ** $b # 1000 (power, PS7+)
# Augmented assignment
$counter = 0
$counter += 1
$counter++ # increment by 1
13
7
30
3.33333333333333
1
Be careful mixing strings and numbers: '5' + 3 returns 53 because the left-hand operand is a string, making + concatenation. Cast explicitly with [int]'5' + 3 to get 8.
Boolean Variables ($true/$false)
Booleans in PowerShell use the built-in automatic variables $true and $false:
$isReady = $true
$hasErrors = $false
if ($isReady) {
Write-Output 'System is ready'
}
# Comparison operators return booleans
$result = (5 -gt 3) # $result is $true
$result
System is ready
True
Automatic Variables ($_, $null, $PSVersionTable)
PowerShell populates several variables automatically that you’ll use constantly:
$_— the current pipeline object insideForEach-ObjectorWhere-Objectscript blocks.$null— represents nothing, similar to null in other languages. Assign it to clear a variable.$PSVersionTable— a hashtable containing version info for the current PowerShell session.$PSCommandPath— the full path of the currently running script file.$Error— an array of recent errors;$Error[0]is the most recent.
# Use $_ in pipeline
1, 2, 3 | ForEach-Object { $_ * 2 }
# Check version
$PSVersionTable.PSVersion
# Clear a variable
$temp = 'something'
$temp = $null
$null -eq $temp # True
2
4
6
Major Minor Build Revision
----- ----- ----- --------
5 1 19041 0
True
Variable Scope Basics
Variables in PowerShell have scope — where they’re visible and accessible:
- Global scope: Available everywhere in the session. Variables declared at the prompt are global.
- Script scope: Variables declared in a script file, only visible within that file.
- Local scope: Variables inside a function are local by default — they don’t leak out.
$global:AppName = 'MyTool' # Explicitly global
function Show-Name {
# This creates a LOCAL $AppName — does not affect the outer scope
$AppName = 'Inner'
Write-Output $AppName
}
Show-Name # Outputs: Inner
$AppName # Outputs: MyTool (unchanged)
To access an outer-scope variable from inside a function, use $script:varName or $global:varName. In practice, pass values through function parameters rather than relying on scope access.
Common Errors and Fixes
-
Missing $ prefix causes ‘not recognized’ error:
serverName = 'Web01'throws a parse error. PowerShell interprets bare words as commands. Always prefix variable names with$. -
Single vs double quotes changing variable expansion:
'Hello $name'outputs the literal textHello $nameinstead of the variable’s value. Switch to double quotes when you need expansion:"Hello $name".
Related Cmdlets / See Also
Wrapping Up
Variables in PowerShell are simple containers: prefix with $, assign with =, and use wherever needed. Use single quotes for literal strings and double quotes when you need variable expansion. Pay attention to scope when writing functions — local variables don’t leak. Your next step: explore how PowerShell’s type system works by reading the data types guide.


