PowerShell includes a collection of specialized miscellaneous operators designed for sequence generation, string tokenization and assembly, null-state evaluation, concise conditional branching, and dynamic command execution. Beyond arithmetic, comparison, and type operators, these specialized operators let you write expressive, compact scripts while avoiding verbose boilerplate code.
The Range Operator (..)
The range operator (..) generates an ordered array of sequential integers between two numbers, inclusive of both endpoints.
Syntax
<start>..<end>The range operator operates in both ascending and descending order:
1..5
5..11
2
3
4
5
5
4
3
2
1where:
1..5produces an ascending sequence.5..1produces a descending sequence automatically, with no special syntax required.
Array Slicing with Ranges
The range operator is frequently used inside index brackets ([]) to slice contiguous segments from arrays:
$Nodes = @("web01", "web02", "sql01", "sql02", "app01")
$Nodes[1..3]web02
sql01
sql02String Operators: -join and -split
The -join and -split operators concatenate collections of items into formatted text strings and break delimited text into arrays of substrings.
The -join Operator
The -join operator merges an array or collection of elements into a single composite string, using a specified delimiter.
- Binary
-join: Concatenates elements with an explicit delimiter between each item ($array -join ", "). - Unary
-join: Concatenates elements with no delimiter (-join $array).
$colors = @("Red", "Green", "Blue")
$colors -join ", "
$Octets = 192, 168, 1, 100
$Octets -join "."
-join @("P", "o", "w", "e", "r")Red, Green, Blue
192.168.1.100
Powerwhere:
-join ", "inserts", "between each array element.- Omitting the delimiter (
$colors -join "") concatenates elements with no separator.
The -split Operator
The -split operator divides a string into an array of substrings, based on a delimiter or regex pattern.
- Binary
-split: Splices a string using an explicit delimiter pattern ($str -split ","). - Unary
-split: When placed before a string with no delimiter specified, it splits text using any sequence of whitespace characters (spaces, tabs, newlines).
"app01,192.168.1.10,Running" -split ","
-split "Server Status Uptime"app01
192.168.1.10
Running
Server
Status
Uptime-split also supports full regex patterns for more complex splitting logic:
"one1two22three333four" -split "\d+"one
two
three
fourwhere \d+ matches one or more consecutive digits, splitting the string at each occurrence.
Null-Coalescing Operators (?? and ??=)
Introduced in PowerShell 7.0, the null-coalescing operators provide a concise syntax for detecting and handling $null values.
Null-Coalescing Operator (??)
The ?? operator returns the value of its left-hand operand if it is not $null. If the left-hand operand is $null, it evaluates and returns the right-hand operand:
$UserSuppliedPort = $null
$ActivePort = $UserSuppliedPort ?? 8080
$ActivePort
$CustomPort = 9000
$ActivePort = $CustomPort ?? 8080
$ActivePort8080
9000where ?? short-circuits: if the left-hand side is not $null, the right-hand side is never evaluated.
Null-Coalescing Assignment Operator (??=)
The ??= operator assigns the value of the right-hand operand to the left-hand variable only if the left-hand variable currently evaluates to $null:
$ConfigPath = $null
$ConfigPath ??= "C:\Configs\default.json"
$ConfigPathC:\Configs\default.jsonThe Ternary Operator (?:)
Introduced in PowerShell 7.0, the ternary operator (?:) provides a compact, single-line alternative to a simple if/else block.
Syntax
<condition> ? <if-true> : <if-false>$age = 20
$status = $age -ge 18 ? "Adult" : "Minor"
Write-Output $statusAdultwhere:
- The condition (
$age -ge 18) is evaluated first. - If
$true, the value before:is returned; otherwise, the value after:is returned. - Ternary expressions evaluate lazily: only the branch corresponding to the evaluated condition is executed.
$CpuLoad = 88
$Alert = ($CpuLoad -ge 80) ? "CRITICAL: $CpuLoad% load" : "NORMAL: $CpuLoad% load"
$Alertpwsh ./ternary-demo.ps1CRITICAL: 88% loadThe Call Operator (&)
The call operator (also known as the invocation operator, &) executes a command, function, script block, or executable file referenced by a string or stored within a variable.
Why the Call Operator Is Required
When a file path contains spaces, enclosing the path in quotes causes PowerShell to treat it as a literal string rather than an executable command:
For example:
"C:\Program Files\PowerShell\7\pwsh.exe" -vThis produces a parser error because PowerShell interprets the quoted path as a string rather than as an executable command:
ParserError:
Unexpected token '-v' in expression or statement.To execute a command stored in a quoted path, use the call operator &:
& "C:\Program Files\PowerShell\7\pwsh.exe" -vThe executable is then invoked with -v as an argument:
7.x.xThe call operator is particularly useful when the command path is stored in a variable:
$PowerShell = "C:\Program Files\PowerShell\7\pwsh.exe"
& $PowerShell -vWithout &, PowerShell treats $PowerShell as a string value. With &, PowerShell invokes the executable represented by that value.
Executing Dynamic Script Blocks
The call operator can also execute raw script blocks ({ ... }) held in memory or variables:
$Action = { Get-Date -Format "yyyy-MM-dd" }
& $Action2026-09-29where:
- Without
&, PowerShell would treat the variable as a plain string or object and simply print it, rather than executing it. &is especially useful when the command to run is determined dynamically at runtime, such as from a variable or configuration value.
Practical Example: Dynamic Dispatcher and Host Validator
The following script combines the range operator (..), string splitting and joining (-split, -join), null-coalescing (??), the ternary operator (?:), and the call operator (&) to generate a batch of target hostnames and execute a ping test:
param (
[string]$SubnetPrefix = $null,
[string]$CommandTarget = "Test-Connection"
)
if ([string]::IsNullOrEmpty($SubnetPrefix)) {
$Subnet = "192.168.1"
}
else {
$Subnet = $SubnetPrefix
}
$IPList = 10..12 | ForEach-Object {
"$Subnet.$_"
}
Write-Output "Target Pool: $($IPList -join ' | ')"
$Results = foreach ($IP in $IPList) {
$PingSuccess = & $CommandTarget -TargetName $IP -Count 1 -Quiet -ErrorAction SilentlyContinue
# PowerShell 5.1 equivalent of the ternary operator
if ($PingSuccess) {
$StatusLabel = "REACHABLE"
}
else {
$StatusLabel = "UNRESPONSIVE"
}
$Octets = $IP -split '\.'
$NodeId = $Octets[-1]
[PSCustomObject]@{
NodeId = "Host-$NodeId"
IPAddress = $IP
State = $StatusLabel
IsOnline = $PingSuccess
}
}
$Results | Format-Table -AutoSizewhere:
$SubnetPrefix ?? "192.168.1"provides a default subnet if none was specified.10..12uses the range operator to generate host numbers 10, 11, and 12.$IPList -join ' | 'formats the generated IP collection into a single display string.& $CommandTargetdynamically executes theTest-Connectioncmdlet stored in the string variable.$PingSuccess ? "REACHABLE" : "UNRESPONSIVE"sets the status tag using an inline ternary check.$IP -split '\.'parses the IPv4 address into its individual octets using regular expression splitting.
Execute the script from the console:
pwsh ./batch-node-dispatcher.ps1Target Pool: 192.168.1.10 | 192.168.1.11 | 192.168.1.12
NodeId IPAddress State IsOnline
------ --------- ----- --------
Host-10 192.168.1.10 REACHABLE True
Host-11 192.168.1.11 UNRESPONSIVE False
Host-12 192.168.1.12 UNRESPONSIVE FalseQuick Reference
| Operator | Purpose |
|---|---|
.. |
Generates a numeric range (ascending or descending) |
-join |
Combines array elements into a single string |
-split |
Splits a string into an array |
?? |
Returns a fallback value when the left-hand side is $null |
??= |
Assigns a value only if the variable is currently $null |
?: |
Compact inline conditional (ternary) expression |
& |
Invokes a command, script, or script block from a variable or string |