---
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/publisher/3ec5d9c85b2b8183bec6e5cc0f0356c5/a323a4d03629188de1d5ed18ca60a64a65f94bef73d5390488bde5511859fa05.webp)

### [AZ-800 vs. AZ-802: Avoid the Wrong Windows Server Exam](/az-800-vs-az-802/)

See what the AZ-800 retirement means for your Windows Server certification path and how to re-weight your study plan around the current AZ-802 exam.

![](https://adamtheautomator.com/wp-content/uploads/publisher/3ec5d9c85b2b811089d5d31807a89aa3/4f6fca0bf1e9083ef5217f16ceed8f939aa4f322e0b4be8d8e3dbb7be8ca3114.webp)

### [Automate Windows 11 Context Menu Actions with Registry](/automate-windows-11-context-menu-registry/)

Add custom right-click actions to Windows 11 context menus by editing registry verbs, deploying .reg files, and scripting with PowerShell.

![](https://adamtheautomator.com/wp-content/uploads/publisher/3ec5d9c85b2b81d9ab7cd5dbb6caf044/21b786d67f19d1813150edf9871e36ead78714b63880de8c6161c7fde11a9769.webp)

### [Pktmon: Capture Windows Traffic Without a Third-Party Driver](/pktmon-capture-windows-traffic/)

Use the in-box Windows Pktmon tool to capture packets and packet-drop reasons, then convert the ETL to pcapng and text for offline analysis.

## 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/)
