PowerShell has evolved significantly over the years, and with the release of PowerShell 7, it introduced several modern operators that make scripting more efficient, readable, and aligned with other popular programming languages like C# and JavaScript.

This guide provides a deep dive into these new operators, showing how I use them to simplify my code and make my scripts more robust.

The key operators I’ll cover are:

  • && and ||Pipeline chain operators
  • ?? and ??=Null-coalescing operators
  • ?:Ternary operator

1. Conditional Execution: The Pipeline Chain Operators (&& and ||)

Introduced: PowerShell 7.0

These operators allow me to execute a command conditionally based on the success or failure of the previous one, just like in Bash or other shells.

  • && (AND): Executes the next command only if the previous command succeeds.
  • || (OR): Executes the next command only if the previous command fails.

In PowerShell, “success” is determined by the exit code of the previous command. A command is considered successful if its exit code is 0. Any non-zero exit code is considered a failure.

Before: The if Statement

Traditionally, I would need an if statement to check the success of a command.

hugo version
if ($LASTEXITCODE -eq 0) {
    wrangler pages deploy public/ --project-name=my-project
}

After: Using &&

The && operator makes this much more concise.

# This command will only run if 'hugo version' succeeds
hugo version && wrangler pages deploy public/ --project-name=my-project

Example: Handling Failures with ||

The || operator is perfect for fallback actions.

# This command will only run if 'hugo version' fails
hugo version || sudo apt install -y hugo

2. Handling null Values: The Null-Coalescing Operators (?? and ??=)

Introduced: PowerShell 7.0

These operators provide a clean and simple way to handle null values without verbose if statements.

  • ?? (Null Coalescing): Returns the value of its left-hand operand if it isn’t null; otherwise, it returns the value of the right-hand operand.
  • ??= (Null Coalescing Assignment): Assigns the value of the right-hand operand to the left-hand operand only if the left-hand operand is null.

Before: The if Statement for Null Checks

$user = Get-ADUser -Identity "PSS1001" -Properties GivenName -ErrorAction SilentlyContinue
if ($null -eq $user.GivenName) {
    $firstName = "Unknown"
} else {
    $firstName = $user.GivenName
}
Write-Host "First Name: $firstName"

After: Using ?? for Safe Defaults

$user = Get-ADUser -Identity "PSS1001" -Properties GivenName -ErrorAction SilentlyContinue
$firstName = $user.GivenName ?? "Unknown"
Write-Host "First Name: $firstName"

Example: Default Assignment with ??=

This is incredibly useful for setting default values for variables that may not have been initialized.

# If $outputPath is not already set, assign it a default value
$outputPath ??= "C:\Reports\ADUsers.csv"
Write-Host "Exporting to: $outputPath"

3. Compact if-else: The Ternary Operator (?:)

Introduced: PowerShell 7.2

The ternary operator is a compact, inline if-else statement that allows me to choose one of two values based on a condition.

Syntax: <condition> ? <if-true> : <if-false>

Before: The Multi-line if-else Block

$user = Get-ADUser -Identity "PSS1001" -Properties Enabled
if ($user.Enabled) {
    $status = "Active"
} else {
    $status = "Disabled"
}
Write-Host "User PSS1001 is $status"

After: Using the Ternary Operator

$user = Get-ADUser -Identity "PSS1001" -Properties Enabled
$status = $user.Enabled ? "Active" : "Disabled"
Write-Host "User PSS1001 is $status"

Putting It All Together: A Robust Script

These operators are most powerful when combined, allowing me to write safe, readable, and concise scripts.

This example safely gets a user, checks for null values, and uses the ternary operator to determine their status, all in a very compact block.

$user = Get-ADUser -Identity "PSS1001" -Properties GivenName, Enabled -ErrorAction SilentlyContinue

$name = $user.GivenName ?? "User not found"
$status = $user ? ($user.Enabled ? "Active" : "Disabled") : "N/A"

Write-Host "User: $name, Status: $status"

Conclusion

The introduction of pipeline chain, null-coalescing, and ternary operators in PowerShell 7 has been a game-changer for scripters. They allowed me to:

  • Write less code: Reduce verbose if statements to single, elegant lines.
  • Improve readability: Make my code’s intent clearer at a glance.
  • Handle errors and nulls gracefully: Build more robust and predictable scripts.

By adopting these modern operators, I can elevate my PowerShell scripting, making it faster to write and easier to maintain.