---
title: "Simplify Conditional Logic with PowerShell Ternary Operators"
description: "Make your PowerShell scripts cleaner and more efficient by mastering ternary operators. Learn the shorthand of conditional logic."
canonical: "https://adamtheautomator.com/powershell-ternary/"
---

# Simplify Conditional Logic with PowerShell Ternary Operators

> Make your PowerShell scripts cleaner and more efficient by mastering ternary operators. Learn the shorthand of conditional logic.

Source: https://adamtheautomator.com/powershell-ternary/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Simplify Conditional Logic with PowerShell Ternary Operators](https://adamtheautomator.com/wp-content/uploads/2019/06/5d238ae5c824b514689ea559.jpg)

# Simplify Conditional Logic with PowerShell Ternary Operators

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)26 June 20191 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [If/Then in PowerShell](#ifthen-in-powershell)
*   [Finishing off the PowerShell Ternary Operator](#finishing-off-the-powershell-ternary-operator)

The if/then construct is commonplace in PowerShell code but did you know there’s another way called the ternary operator that allows you to make your if/then constructs much more concise? Let’s learn how to build a custom PowerShell ternary operator.

At the expense of readability, some say, the ternary operator builds conditional logic that’s more concise, simpler, and with less code. They’re correct but it’s just so nice to have that ternary-like behavior in PowerShell!

If you’re not familiar with a [ternary operator](https://guide.freecodecamp.org/c/ternary-operator/), it’s basically using a hashtable or similar construct to make a conditional decision based on criteria.

## If/Then in PowerShell

To explain the [Powershell](https://adamtheautomator.com/tag/powershell/) ternary operator, let’s first start off with an example of an if/then construct.

```powershell
$CarColor = 'Blue'
if ($CarColor -eq 'Blue') {
    'The car color is blue'
} else {
    'The car color is not blue'
}
```

Related: [Back to Basics: Conditional Logic with PowerShell If-Else](https://adamtheautomator.com/powershell-if-else/)

At first glance, you might not think there’s anything wrong with it. In fact, there really _isn’t_ but this condition could be tested just as easily with a single line (under my personal limit of 115 characters).

Now build a PowerShell hashtable with two keys; `$true` and `$false`. Then, make the values what you’d like to be displayed if a condition you define is met.

```powershell
@{ $true = 'The car color is blue'; $false = 'The car color is not blue' }
[$CarColor -eq 'Blue']
```

Related:[Understanding PowerShell Comparison Operators By Example](https://adamtheautomator.com/powershell-like/)

Next, define the condition (`$CarColor is Blue`) and check to see if that condition is met with `$CarColor -eq 'Blue'`.

```powershell
$CarColor = 'Blue'
@{ $true = 'The car color is blue'; $false = 'The car color is not blue'}
$CarColor -eq 'Blue'
```

Now use the condition (`$CarColor -eq 'Blue'`) as a key in that hashtable. Doing this performs the check and then uses the result to look up the key in the hashtable.

## Finishing off the PowerShell Ternary Operator

```powershell
$CarColor = 'Blue'
@{ $true = 'The car color is blue'; $false = 'The car color is not blue'}[$CarColor -eq 'Blue']
```

![A custom PowerShell ternary operator](https://adamtheautomator.com/wp-content/uploads/2022/04/image-53-1024x71.png)

A custom PowerShell ternary operator

One line! Isn’t that much more succinct? Instead of using an if/then statement I’m using a hashtable and performing a lookup based on if `$CarColor` equals `Blue` or not. The resulting index is then output to the console. If you’d like to use this method, it’s just as simple as filling in these blanks:

```powershell
@{$true = $ResultyouwanttodoifTrue; $false = $ResultyouwantifFalse}[]
```

You could also include more than just `$true` and `$false` if you’d like. You could add any number of conditions in the hashtable and check for them. It’s an easy way to replace long if/then statements or switch statements.

You now have a custom PowerShell ternary operator you can start using in your scripts today!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-ternary%2F&text=Simplify%20Conditional%20Logic%20with%20PowerShell%20Ternary%20Operators)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-ternary%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-ternary%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
