Skip to content

Instantly share code, notes, and snippets.

@sassdawe
Last active April 13, 2025 19:14
Show Gist options
  • Save sassdawe/0d69d05339e0e5c7a1ee18a9926bf759 to your computer and use it in GitHub Desktop.
Save sassdawe/0d69d05339e0e5c7a1ee18a9926bf759 to your computer and use it in GitHub Desktop.
Ternary operator for Windows PowerShell v2 and beyond, maybe even for v1
<##################################################################################
#
# Script name: ternary.ps1
# source http://blogs.technet.com/b/heyscriptingguy/archive/2009/06/15/hey-scripting-guy-event-2-solutions-from-expert-commentators-beginner-and-advanced-the-long-jump.aspx
#
##################################################################################>
set-alias ?: Invoke-Ternary -Option AllScope -Description "PSCX filter alias"
filter Invoke-Ternary ([scriptblock]$decider, [scriptblock]$ifTrue, [scriptblock]$ifFalse) {
if (&$decider) {
&$ifTrue
}
else {
&$ifFalse
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment