Skip to content

PowerShell Other Operators

How PowerShell's specialized operators like .., -join, -split, ??, ?:, and & simplify sequence generation, string manipulation, null handling, conditional logic, and dynamic command execution.

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:

Consolepowershell
1..5

5..1
Outputplaintext
1
2
3
4
5
5
4
3
2
1

where:

  • 1..5 produces an ascending sequence.
  • 5..1 produces 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:

Consolepowershell
$Nodes = @("web01", "web02", "sql01", "sql02", "app01")

$Nodes[1..3]
Outputplaintext
web02
sql01
sql02

String 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).
Consolepowershell
$colors = @("Red", "Green", "Blue")
$colors -join ", "

$Octets = 192, 168, 1, 100

$Octets -join "."

-join @("P", "o", "w", "e", "r")
Outputplaintext
Red, Green, Blue
192.168.1.100
Power

where:

  • -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).
Consolepowershell
"app01,192.168.1.10,Running" -split ","

-split "Server    Status      Uptime"
Outputplaintext
app01
192.168.1.10
Running
Server
Status
Uptime

-split also supports full regex patterns for more complex splitting logic:

Consolepowershell
"one1two22three333four" -split "\d+"
Outputplaintext
one
two
three
four

where \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:

Consolepowershell
$UserSuppliedPort = $null
$ActivePort = $UserSuppliedPort ?? 8080
$ActivePort

$CustomPort = 9000
$ActivePort = $CustomPort ?? 8080
$ActivePort
Outputplaintext
8080
9000

where ?? 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:

Consolepowershell
$ConfigPath = $null
$ConfigPath ??= "C:\Configs\default.json"
$ConfigPath
Outputplaintext
C:\Configs\default.json

The 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>
Consolepowershell
$age = 20
$status = $age -ge 18 ? "Adult" : "Minor"
Write-Output $status
Outputplaintext
Adult

where:

  • 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.
ternary-demo.ps1powershell
$CpuLoad = 88
$Alert = ($CpuLoad -ge 80) ? "CRITICAL: $CpuLoad% load" : "NORMAL: $CpuLoad% load"
$Alert
Consolepowershell
pwsh ./ternary-demo.ps1
Outputplaintext
CRITICAL: 88% load

The 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:

Consolepowershell
"C:\Program Files\PowerShell\7\pwsh.exe" -v

This produces a parser error because PowerShell interprets the quoted path as a string rather than as an executable command:

Outputplaintext
ParserError:
Unexpected token '-v' in expression or statement.

To execute a command stored in a quoted path, use the call operator &:

Consolepowershell
& "C:\Program Files\PowerShell\7\pwsh.exe" -v

The executable is then invoked with -v as an argument:

Outputplaintext
7.x.x

The call operator is particularly useful when the command path is stored in a variable:

$PowerShell = "C:\Program Files\PowerShell\7\pwsh.exe"

& $PowerShell -v

Without &, 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:

Consolepowershell
$Action = { Get-Date -Format "yyyy-MM-dd" }

& $Action
Outputplaintext
2026-09-29

where:

  • 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:

batch-node-dispatcher.ps1powershell
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 -AutoSize

where:

  • $SubnetPrefix ?? "192.168.1" provides a default subnet if none was specified.
  • 10..12 uses the range operator to generate host numbers 10, 11, and 12.
  • $IPList -join ' | ' formats the generated IP collection into a single display string.
  • & $CommandTarget dynamically executes the Test-Connection cmdlet 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:

Consolepowershell
pwsh ./batch-node-dispatcher.ps1
Outputplaintext
Target 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    False

Quick 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
Navigation