Scalar Variables¶
A scalar variable in PowerShell is a variable that holds a single value at a time. The value can be of any type (string, integer, boolean, object, etc.), but the variable itself represents one item, not a collection. Scalar variables are the fundamental building blocks of PowerShell scripting, used to store intermediate results, configuration values, user input, and command output.
Declaring and assigning scalar variables¶
Scalar variables are created by assigning a value to a name prefixed with $:
$number = 42
$text = "Hello, PowerShell"
$isEnabled = $true
$pi = 3.14159
PowerShell is dynamically typed: the type of the variable is inferred from the assigned value. You can also declare the type explicitly:
[int] $count = 10
[string]$name = "Admin"
[bool] $flag = $false
Explicit typing is useful when you want to enforce a specific type or avoid implicit conversions.
Reading and updating scalar variables¶
Scalar variables are referenced by name:
Write-Output $text
Write-Output $number
You can update their value at any time:
$number = $number + 1
$text = $text + "!"
Each assignment replaces the previous value; the variable still holds a single value.
Scalars vs. collections¶
A scalar variable holds one value, while a collection (such as an array) holds multiple values:
$scalar = 5
$array = 1, 2, 3, 4, 5
Even if a scalar variable receives the result of a command that returns multiple items, PowerShell still treats the variable itself as a single container:
$processes = Get-Process
Here, $processes is a scalar variable that holds a collection (an array-like object) of process objects. The variable is scalar; the value it contains is a collection. This distinction is important: scalar describes the variable’s role (one slot), not necessarily the nature of the value (single item vs. multiple items).
Common scalar types and usage¶
Strings¶
Strings store text:
$message = "Backup completed successfully."
$path = "C:\Data\Report.txt"
Typical usage:
- File paths
- Log messages
- Usernames and identifiers
Integers and numeric types¶
Numeric scalars store whole numbers or floating‑point values:
[int] $retryCount = 3
[double]$threshold = 0.75
Typical usage:
- Counters and loop indices
- Thresholds and limits
- Numeric configuration values
Booleans¶
Booleans represent logical values:
[bool]$isAdmin = $true
[bool]$isEnabled = $false
Typical usage:
- Conditional logic (
if,while) - Feature flags
- Status indicators
Objects¶
Scalar variables can hold complex objects:
$process = Get-Process -Name "powershell"
$user = Get-LocalUser -Name "Administrator"
These variables still hold a single value, but that value is an object with properties and methods.
Scalar variables in expressions and operators¶
Scalar variables are frequently used with operators:
$a = 10
$b = 20
$sum = $a + $b
$isEqual = ($a -eq $b)
$isGreater = ($a -gt 5)
Examples of usage:
- Arithmetic:
+, , ,/ - Comparison:
eq,ne,gt,lt,ge,le - Logical:
and,or,not
Scalar variables participate directly in these expressions, and PowerShell performs type‑appropriate operations.
Scalar variables and command output¶
Scalar variables often store the result of commands:
$currentUser = [System.Environment]::UserName
$service = Get-Service -Name "Spooler"
$time = Get-Date
These values are then reused in later commands:
Write-Output "Current user: $currentUser"
Write-Output "Service status: $($service.Status)"
Write-Output "Current time: $time"
This pattern—capture, store, reuse—is central to scripting.
Summary of main usage¶
Scalar variables are used to:
- Store single configuration values (paths, thresholds, flags)
- Hold the result of commands for later processing
- Serve as counters and state holders in loops and conditions
- Represent single objects (a process, a service, a user)
They provide the basic “named storage” that makes scripts readable, maintainable, and expressive.